I'm trying to replace some characters in a textfile.
Code:
import easygui
textfile = easygui.enterbox(msg='File:') # only used for input
f1 = open(textfile, 'r')
f2 = open(textfile, 'w')
for line in f1:
f2.write(line.replace('a', '-apple-')), # also tried without comma; same result
f2.write(line.replace('t', '-tree-'))
f1.close()
f2.close()
File:
this is a test!
But the output is messed up and instead of replace the letters in one go, the script creates multiple sentences in the file like:
this is -apple- test!-tree-his is a -tree-es-tree-!
Is it even possible to replace multiple letters in only one line?