I've written a small script to append a character to the end of each line of a text file, "," in my case.
This will be shared with my team, so rather than have them update the script itself I thought it would be easier to simply define a variable at the start they can change.
However, it takes 'char' as a literal string and appends 'char' rather than ',' in the test file. If I swap char\n
back to ,\n
it appends a comma correctly.
How do I fix this?
file = 'test.txt' # file to append text to, keep the ''
char = ','
newf=""
with open(file,'r') as f:
for line in f:
newf+=line.strip()+'char\n'
f.close()
with open(file,'w') as f:
f.write(newf)
f.close()