python - edit a text file

2019-08-27 07:01发布

I am using python and i want to delete some characters from the end of a text file. The file is big a and i dont want to read all of it and duplicate the interesting part. My best guess is that i need to change the file size....

can anyone help me please thanks

1条回答
姐就是有狂的资本
2楼-- · 2019-08-27 07:26

I guess that you need to open the file, seek to the end, delete characters and save it.

seek ( http://docs.python.org/library/stdtypes.html#file.seek ) accepts negative values (e.g. f.seek(-3, os.SEEK_END) sets the position to the third to last), so that you can easily go to the end of your file.

http://docs.python.org/library/stdtypes.html#file-objects - this link may be a good starting point.

查看更多
登录 后发表回答