python - 编辑文本文件 [英] python - edit a text file

查看:30
本文介绍了python - 编辑文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python,我想从文本文件的末尾删除一些字符.该文件很大,我不想阅读所有内容并复制有趣的部分.我最好的猜测是我需要更改文件大小....

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....

谁能帮帮我谢谢

推荐答案

我猜你需要打开文件,查找到最后,删除字符并保存.

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 ) 接受负值(例如 f.seek(-3, os.SEEK_END) 将位置设置为倒数第三个),以便您可以轻松地到文件末尾.

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 - 这个链接可能是一个很好的起点.

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

这篇关于python - 编辑文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆