Remove very last character in file

前端 未结 7 1831
说谎
说谎 2020-11-29 08:00

After looking all over the Internet, I\'ve come to this.

Let\'s say I have already made a text file that reads: Hello World

Well, I want to remo

7条回答
  •  孤独总比滥情好
    2020-11-29 08:28

    In case you are not reading the file in binary mode, where you have only 'w' permissions, I can suggest the following.

    f.seek(f.tell() - 1, os.SEEK_SET)
    f.write('')
    

    In this code above, f.seek() will only accept f.tell() b/c you do not have 'b' access. then you can set the cursor to the starting of the last element. Then you can delete the last element by an empty string.

提交回复
热议问题