python - edit a text file

2019-08-27 06:34发布

问题:

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:

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.