Thursday, July 17, 2014

python: open a file to read, then replace string, write back to file

f = open('file.txt', 'r');
s = f.read();
print s
s = s.replace('\n', '\\n');
s = s.replace('\r', '');
# print s
f.close();
f = open('file_replaced.txt', 'w');
f.write(s);
f.close();

print "file written"

No comments: