Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.


This page removed for FERPA compliance
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm Exam 2 Review Spring 2003: Reverse a file

Comments? Concerns? Answers? Questions? Questions about Answers?
Back to Sp2003 Midterm Review 2


Does anyone understand how to do this?



Think about the backwards sound function. It's the same thing. Mark Guzdial


You can ignore this, I'm still working on it.
def reverseFile(“file”):
  file=open(“file”,rt)
  lines=file.readlines()
  print lines
  file.close()

Why would you put the input variable in quotes? Mark Guzdial


Since you're turning it into a list, I'm thinking maybe you can use reverse() somehow...this isn't correct, but maybe it will lead somebody else in the right direction, because I am definitly confused.

def reverseFile(file):
file = open(file, "rt")
lines = file.readlines()
print lines
file.close()
print reverse(lines)

Student117
Nice start, REMOVEDmmer! Did you try it? There is no function named reverse(). There is a list method reverse(). Mark Guzdial
>>> a=[1,2,3]
>>> a.reverse()
>>> a
[3, 2, 1]

(But you can also write reverse yourself – you know how from doing backwards.) Mark Guzdial


do we need to use the .find(findstring) functions to identify the values of the last word or line and then use the print function to enter and print them in reverse order?

for example, here's what i sort-of have...


def reverseFile(file):
original = open(file,"rt")
lines = original.readlines()
print lines
print lines.find(findstring,end)
#um, here's where the rest of the code goes that i can't figure out
original.close()

Student68



def reverseFile(file):
  file = open(file,"rt")
  lines = file.readlines()
  file.close()
  length = len(lines)
  newfile = lines.reverse
  file = open("reversed.txt", "wt")
  file.write(newfile)

I can't figure out how to create the file "reversed" to be written to. Other than that, this is what i have.
Katie Graybeal

def reverseFile(file):
  openfile = open(file,"rt")
  lines = openfile.readlines()
  openfile.close()
  nolines = len(lines)
  wfile = "reversed.txt"
  writefile = open(wfile,"wt")
  for index in range(1,len(lines)+1):
    writeline = nolines - index
    line = lines[writeline]
    # Put new line after last (new first) line if it doesn't have one
    if line.find("\n") == -1:
      line = line + "\n"
    writefile.write(line)
  writefile.close()

Try this; it works for me; it outputs the result to reversed.txt
Student141

That works, REMOVED, but is pretty complicated. Someone want to tweak Katie's? It's very close. First of all, it's reverse(), not just reverse. (It's a method call – you need parentheses.) Put in a For loop lineNumber in range(0,length+1), then write out to the file newfile[lineNumber] (as a string) + "\n". Mark Guzdial


Ok, here is a simpler version of it. This uses the reverse, but it doesn't check to see if the last line has a \n at the end, so if the last line doesnt end with a \n, it will have them both on the same line of the text file. If I add a "\n" to the end in all cases, it adds another "\n" on top of the one that is already there, so it would have two spaces. If you want, you can copy the code I put above to test for that and it will work fine (change the tabbing of course). (Also note, of course, that I changed the name of the function because this way it wont conflict with my other function which I have loaded at the same time in my JES)
def reverseSimple(filename):
  file = open(filename,"rt")
  lines = file.readlines()
  file.close()
  newfile = "reversed.txt"
  file = open("reversed.txt","wt")
  lines.reverse()
  for line in lines:
    file.write(line) 
  file.close()
Student141

shouldnt it be reverseSimple(): and not (filename)? If not, please explain. Mine only works like (): Kelly Farrell

No, because you are opening the filename that you specify in the input. If you specify nothing, it wont know which file to open where it says open(filename,"rt"). If you hard code in a filename, it should work, but then its less dynamic of a program and can only reverse that particular file. If you take filename as input, it is REMOVED generalized and will work for any text file that you give it. Student141

Why not:
def reverseSimple(filename):
  file = open(filename,"rt")
  lines = file.readlines()
  file.close()
  newfile = "reversed.txt"
  file = open("reversed.txt","wt")
  lines.reverse()
  for line in lines:
    file.write(line+"\n") 
  file.close()



oh, so it will work either way, but it will be easier on the user if you put (filename): and not just (): I see. I have since gotten it to work that way for me. Thanks for the help. Kelly Farrell

You should know this for the final! Mark Guzdial





Links to this Page