8

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?

1
  • If you modify the head, you will need to write the rest of the file after the first line. It's just the way filesystems work. Commented Jun 30, 2011 at 16:14

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

yeah, I think it is not something I could avoid.. thank you all
1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.