Where am I going wrong ??
import os, os.path, re
path = "D:\python-test"
myfiles = os.listdir(path)
REGEXES = [(re.compile(r'dog'), 'cat'),
(re.compile(r'123'), '789')]
for f in myfiles:
file_name, file_extension = os.path.splitext(f)
if file_extension in ('.txt', '.doc', '.odt', '.htm', '.html', '.java'):
input_file = os.path.join(path, f)
with open(input_file, "w") as fi:
for line in fi:
for search, replace in REGEXES:
line = search.sub(replace, line)
fi.write(line)
Somehow its not working. I want to make the replacements in the current file and not a new file.
Update: How about creating A_reg.java from A.java. Moving A.java to a separate local folder and then renaming A_reg.java back to A.java . Possible ? If yes, please help me out with the code.
'w'mode, it is truncated. user+ora+modes.