 |  |

 |
 |  |  |
 | Welcome to CS1315. Click on the python to add comments.
|  |
 |  |  |
|
This page removed for FERPA compliance
|
        |
Midterm Exam 2 Review Spring 2004: Reverse a file
Comments, answers, and questions go here:
(REMOVEDnk back to Sp2004 Midterm 2 Review)
I know this is way off, but got to start somewhere. Any hints would be greatly appreciated!! Thanks!
def reverseFile():
file=open()
lines=file.readREMOVEDnes()
reverse=list.reverse()
return()
list.writeTo("reverse.txt")
Catherine Covington
Should we make the lines into a list and then return the list backwards? Is that anywhere close to the right idea?
-Rachel REMOVED
is this anywhere close??
def reverseFile(“file”)
file=open(file, “rt”)
lines=file.readlines()
lineslist=lines.split(‘\n’)
newlist=lineslist.reverse()
writeTo: “what goes here?”
file.close()
Amelia Cipolla
import os
def reverseFile(file):
myfile = open(file,"rt")
lines=myfile.readlines()
myfile.close()
lines.reverse()
newfile = open("reverse.txt", "wt")
for line in lines:
newfile.write(line)
newfile.close()
Student933
def reverseFile(filename):
file = open(filename,"rt")
fileContents = file.readlines()
file.close()
reverseContents = fileContents.reverse()
reversedFile = open(reverseContents, "wt")
for item in fileContents:
reversedFile.write(item)
reversedFile.close()
print fileContents
def reversefile():
file = open(file, "rt")
contents = file.readlines()
file.close()
contents.reverse()
file = open("reversed.txt","wt")
for item in contents:
file.write(item)
file.close()
TA's, anyone able to tell us if there are problems with these? No wink-winks like we're on the right track?
| Why don't you try them? The last one is pretty close, except that file = open(file,"rt") won't work. What's "file"? Mark Guzdial |
I've tried all of these and nothing is happening but I can't figure out why.. Should the reverse() come after the file is opened for writing?? Jennifer Garrett
I created a txt file and inserted it's name where"file" is in the open statement and it worked. For some reason, though, if I tried to put the file name in the def statement in the () it does not work. Why?
if you want to put the filename in using the command area then it should be:
def reversefile(file):
As shown above it is not recognizing anything in the parenthesis as a variable. I could be wrong, correct me if i am.
Ok, I got part of it but i'm having a small problem. This is what i am using as code.
def reversefile(file):
file = open(file, "rt")
contents = file.readlines()
file.close()
contents.reverse()
file = open(r"d:\reversed.txt","wt")
for item in contents:
file.write(item)
file.close()
and I am inputing this:
hello
to
cs 1315
everyone
Problem is that I get this back:
everyonecs 1315
to
hello
Ideas?
Student1189
REMOVED,
I wrote the same code as you, and I have the same problem. Sorry I can't help, but I just wanted you to know someone else was feeling your pain.
Student1063
| REMOVED and Christina, you didn't hit Enter on the last line. So when that line is put first, there is no \n at the end of it, so the next line just whumps on top of it. It's no problem – you did it right! Mark Guzdial |
Link to this Page