how to remove the very last character of a text file in python

Solutions on MaxInterview for how to remove the very last character of a text file in python by the best coders in the world

showing results for - "how to remove the very last character of a text file in python"
Rebeca
23 Sep 2019
1file.truncate(file.tell - 1)
2#explanation - the function truncate will resize the file to the 
3#full size - 1 and that means it will remove the last character
4#if you need to do that while you are reading/writing somewhere using
5#seek() you can use this function ->
6def get_size(fileobject):
7    fileobject.seek(0,2) # move the cursor to the end of the file
8    size = fileobject.tell()
9    return size
10#and then
11fsize = get_size(file)
12file.truncate(fsize - 1)
13