有效的方法来改变Python中的文件头 [英] efficient way to change the header of a file in Python
问题描述
不,您可以在文件上执行的唯一操作无需触及整个文件即可截断,替换相同的大小,然后追加。
您可以,但是,在读取当前驻留在新位置的所有数据之后,缓冲文件的相对较小的部分并写入,以避免内存耗尽。如果速度有问题,请考虑使用 mmap 。
I am trying to write a python script to update the header (only the first line) of some huge files, but as the new header is not necessary to be the same size (in bytes) as the original one, is there anyway I could change the header without touching the rest of the huge file? or I have to read through them all and write them back to file?
No, the only operations you can do on files without touching the whole file are truncation, replacement of same size, and appending.
You can, however, buffer relatively small parts of the file and write them after you've read all data currently residing in the new position, to avoid memory exhaustion. If speed is an issue, consider using mmap.
这篇关于有效的方法来改变Python中的文件头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!