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 Spring 2004: The Cover-Up

Comments, answers, and questions go here:

(Link back to Sp2004 Midterm 2 Review)


The TA's helped us write this function tonight at the review. I wrote a .txt file with the initials in this function to test the program, but when it runs instead of replacing the initials it erases all the text in the file. Where am I going wrong? Also, for those of you who were there tonight the TA's forgot to put in the +"//"+ in the myfile=open part. You have to have those or you get error messages.
import os
def coverup(directory):
  for file in os.listdir(directory):
    if file.endswith(".txt"):
      myfile=open(directory+"//"+file, "rt")
      contents=myfile.read()
      contents.replace("AW", "MG")
      myfile.close()
      myfile=open(directory+"//"+file, "wt")
      myfile.write(contents)
      myfile.close()


Brittany Copeland




Where should the spacing be on this?


my guess is that you cant replace content when you have only opened a file to read it. Try putting the contents.replace command after you open the file to write in it.
Margaret McIntosh

I got this as an error message;
"An error occurred while making an operating system call. Please tell a TA what you were doing when this happened, so we may correct it."
it stopped and highlighted this line
"for file in os.listdir(directory):"
i had the indentions right and everythin, what's going on??


import os
def coverUp(directory):
for file in os.listdir(directory):
if file.endswith(".txt"):
myfile = open(directory+"//"+file, "rt")
contents = myfile.read()
myfile.close()
myfile = open(directory+"//"+file, "wt")
contents = contents.replace("Brant", "Hope")
myfile.write(contents)
myfile.close()



When you open the file with the "wt" (write text) option, it will delete the content of the file. Furthermore, make sure you note that the replace function will return the modified string.

mystring = "test"
mystring.replace("t", "a")
print mystring

Will print out "test"!!

However, if you want to change a string, you need to write code like this:

mystring = "test"
mystring = mystring.replace("t", "a")
print mystring

Because the replace function only returns the modified string.

Chris McGarty





import os
def coverup(directory):
for file in os.listdir(directory):
if file.endswith(".txt"):
myfile=open(directory+"//"+file, "rt")
contents=myfile.read()
myfile.close()
newcontents=contents.replace("AW", "MG")
myfile=open(directory+"//"+file, "wt")
myfile.write(newcontents)
myfile.close()

Amy Howard

How can we test this funtion? what do we type in the comand area? the import os stuff throws me off....thanks

to test this function, you can simply create a folder on your c: somewhere (like c:\testfld) and then create a text file inside of that folder with a couple lines of text and your initials in it. In JES you just type :
>>>coverUp(r"c:\testfld");

Chris McGarty


Chris, would the answer to your code above, where you are replacing a where t is in the word test, be aesa?

koss imak

iopipoi



Link to this Page