Is there a way in Python to read in a txt file, remove the last 6 characters of each line and overwrite the old file with the same content just without the last 6 chars on each line?
I tried something like this:
with open(dat, "r+") as fp:
for line in fp:
line = line[:-6]
fp.write(line+"\n")
But that only gives me a weird looking file with multiple entries and numbers at the wrong places.
Also
with open(dat, "r+") as fp:
for line in fp:
line = line[:-6]
fp.write(line+"\n")
doesn't work. The whole file is empty when I am doing that.
Is there a smart way of doing this without doing the change, writing everything to a seperate file?