开发者

efficient way to change the header of a file in Python

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 co开发者_开发知识库uld 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.


I'm not familiar with any OS that lets you remove arbitrary chunks of a file, so Python cannot give you that feature. I'm afraid you are stuck touching the rest of the huge file.


You will have to read and write the whole file, since the rest of the contents of the file will have to be moved to accomodate for the differences in header size.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜