I have a config file that i would like to add a space (\' \') to the end of every line in the file
File example:
#xxx configuration
IPad
You can use fileinput with inplace=True
to update the original file:
import fileinput
import sys
for line in fileinput.input("in.txt",inplace=True):
sys.stdout.write("{} \n".format(line.rstrip()))
Or use a tempfile.NamedTemporaryFile with shutil.move:
from tempfile import NamedTemporaryFile
from shutil import move
with open("in.txt") as f, NamedTemporaryFile("w",dir=".", delete=False) as temp:
for line in f:
temp.write("{} \n".format(line.rstrip()))
move(temp.name,"in.txt")