 |  |

 |
 |  |  |
 | Welcome to CS1315. Click on the python to add comments.
|  |
 |  |  |
|
This page removed for FERPA compliance
|
        |
Midterm Exam 2 Review Spring 2005: The Cover-Up
Post your answers, questions, comments, comments on answers, suggestions, suggestions on questions, etc.
Back to Spring2005 Midterm 2 Review
def coverUp(directory):
import os
for file in os.listdir(directory):
if file.endswith(".txt"):
myfile=open(file,"wt")
myfile.replace("ZL","MS")
file.close()
| myfile is a file. This is different from a string. You can only REMOVEDe .replace on strings. Try reading the file to a string first. Student1594 |
def coverUp(directory):
import os
for file in os.listdir(directory):
if file.endswith(".txt"):
myfile=open(directory+"\\"+file,"rt")
content=myfile.read()
myfile.close()
w=content.replace('ZL','MS')
newfile=open(directory+"\\"+file,"wt")
newfile.write(str(w))
newfile.close()
| You shouldn't need to cast w to a string since it already is a string. Otherwise it looks good. Student1594 |
how would this look without the extra step of cast w to a string?
newfile.replace("old initials", "new initials") ??
or would you REMOVEDe a for loop?
Link to this Page