python how to delete last character in each line in a text file code example
Example 1: how to remove the very last character of a text file in python
file.truncate(file.tell - 1)
def get_size(fileobject):
fileobject.seek(0,2)
size = fileobject.tell()
return size
fsize = get_size(file)
file.truncate(fsize - 1)
Example 2: remove last line of text file python
fd=open("file.txt","r")
d=fd.read()
fd.close()
m=d.split("\n")
s="\n".join(m[:-1])
fd=open("file.txt","w+")
for i in range(len(s)):
fd.write(s[i])
fd.close()