I made a simple program to test this out. It simply finds any instance of a specific string, and replaces it with a new string. What I want to do is run this against my entire directory, file by file.
def replace(directory, oldData, newData):
for file in os.listdir(directory):
f = open(file,'r')
filedata = f.read()
newdata = filedata.replace(oldData,newData)
f = open(file, 'w')
f.write(newdata)
f.close()
But I keep getting an error message telling me that one of files doesnt exist in my directory, even though it does. I can't figure out why it would tell me that.
os.listdirjust returns the filenames, they don't have the directory prefix. Useos.path.jointo connect them.