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

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Midterm Exam 2 Review Fall 2003: Reverse a file

Answers? Comments? Questions on Answers? Questions on Comments?

(Back to Fall2003 Midterm Review 2)



"You are trying to access a part of the object that doesn't exist." error again at line "startingString":
Again, because readlines() returns a list, and lists don't understand .find. Mark Guzdial
def reverseFile(filename):
  file = open(filename,"rt")
  fileContents = file.readlines()
  file.close()
  reversedFile = open(filename,"wt")
  for string in fileContents:
    startingString = fileContents.find("\n",string)
    endingString = fileContents.find("'",startingString - 1)
    copyingString = fileContents[startingString-1:ending2String]
    reversedFile.write(copyingString)
    startingString = startingString - 1
  reversedFile.close()
  file = open(filename,"rt")
  print file.read()
  file.close()


If you have all the lines in a list, and lists understand the method reverse(), why don't you just use that? Mark Guzdial


i got it working but with the returns. do u want the return or get rid of the returns and if you do want to get rid of returns, how should i go about doing it :)

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

def reverseFile(filename):
file = open(getMediaPath(filename), "rt")
fileContents = file.readlines()
file.close()
reversed = open("reversed.txt","wt")
fileContents.reverse()
for line in fileContents:
reversed.write(line)
reversed.close()
fileedit = open("reversed.txt","rt")
print fileedit.read()
fileedit.close()

where i put print file.read() i get back the right thing, but i dont know how to make JES actually write this out into a txt called reversed. anybody have any ideas?

since the last line might not have the return '\n', it would mess up the output, I added this line to make sure it has the return at the end:
if (each[len(each)-1:len(each)] <> "\n"):
      each = each + '\n'


if my variable "mylines"=the reverse of the file, how do I write it out to another file?

reverse=open("reverse.txt","wt")
reverse.write("mylines")

this doesnt work. it only writes "mylines" to the file. what am i doing wrong?

Try not putting it in quotes? You're making it a string instead of a variable.

Perhaps try writing each line out separately? Use a loop? Mark Guzdial


finally works.........writes out the revesed of the original to the original text file.
def reverseFile(filename):
  file = open(filename,"rt")
  fileContents = file.readlines()
  file.close()
  fileContents.reverse()
  print fileContents
  reversedFile = open(filename,"wt")
  for item in fileContents:
    reversedFile.write(item)
  reversedFile.close()
  reversedFile = open(filename,"rt")
  fileContents = reversedFile.read()
  reversedFile.close()
  print fileContents 


what does "rt" and "wt" do?

read text and write text maybe???


"rt" means "read text". "wt" means "write text." Yup. Mark Guzdial

When I tried the last suggestion in JES, I got
>>> reverseFile(file)
['file, okay?\n', 'ordinary\n', 'a perfectly\n', 'This is\n']
An error occurred attempting to pass an argument to a function.
Please check line 9 of C:\Documents and Settings\Dina Forehand\My Documents\JES\reverseFile.py
>>>
What happened?

Diana, could you show us the program? What's on line 9? Mark Guzdial

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


How does this look?

Nice. Does it work? Looks right. Mark Guzdial

def reverseFile(filename):
  file = open(filename,"rt")
  fileContents = file.readlines()
  file.close()
  fileContents.reverse()
  print fileContents
  reversedFile = open(filename,"wt")
  for item in fileContents:
    reversedFile.write(item)
  reversedFile.close()
  reversedFile = open(filename,"rt")
  fileContents = reversedFile.read()
  reversedFile.close()
  print fileContents 


I just copied and pasted it from the CoWeb. Dina

emre soytürk




Link to this Page