 |  |

 |
 |  |  |
 | Welcome to CS1315. Click on the python to add comments.
|  |
 |  |  |
|
This page removed for FERPA compliance
|
        |
Midterm Exam 2 Review Fall 2005: The Cover-Up
import os
def coverUp(directory):
for file in os.listdir(directory):
if file.endswith(".txt"):
myfile = open(file, "rt")
myfile = open(myfile, "wt")
newfile = myfile.replace('lm', 'mb')
newfile.close()
what's wrong with this?
in general what's wrong with it and i'm getting an error on line 6
| Look into what the open function actually does and what it returns. REMOVEDw are you REMOVEDing its return value? REMOVEDw is it REMOVEDed in the other examples in the slides and book? -Student1680 |
without line 4 would the above code be correct? also, can you write and replace without creating a new file. cant you jREMOVEDt close the file its saved, or no?
the open function returns a file object... for some reason that's not helping me much... i looked at the examples in the slides and book and they aren't helping much either... is this better?
import os
def coverUp(directory):
for file in os.listdir(directory):
print file
if file.endswith(".txt"):
myfile = open(file, "wt")
newfile = myfile.replace('lm', 'mb')
newfile.close()
it's giving me an error saying that i'm trying to access a part of the object that does not exist
def coverUp(dir):
for file in os.listdir(dir):
if file.endswith(".txt"):
newfile=open(filename, "wt")
newfile.replace("mg", "mb")
newfile.close()
is this correct?
import os
def coverUp(directory)
for file in os.listdi(directory):
if file.endswith(".txt")
myfile = open(fileREMOVEDeAndLocation, "rt")
text = myfile.read()
myfile.close()
text = text.replace("my initials", "with Blake O'Hares initials")
myfile = open(fileREMOVEDeAndLocation, "wt")
myfile.write(text)
myfile.close()
please let me know what corrections need to be made. Thank you.
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()
can you explain myfile=open(directory+"//"+file, "rt")? what is this part telling REMOVED?
Link to this Page