Python - small change to a huge file

This is a theoretical question, since I have no real problem, but I became interested ...

If I had a huge file, say a lot of gigs, and I wanted to change one byte, and I knew the offset of this byte, how could I do this efficiently? Is there a way to do this without overwriting the entire file and writing only one byte?

I do not see anything in the api file in Python that would allow me to write a specific offset in the file.

+5
source share
4 answers

While you do not need to insert or delete bytes, you can open the file in mode "r+", use seekto put the file object in bytes to change, and write out one byte.

, os.open, os.lseek, os.read os.write , .

, , : ( ). API- POSIX ( AFAIK Windows), Python.

+8

() . , , .

+2

. Python , :

seek(offset[, whence])

whence 0 ( ); 1 ( ) 2 ( ).

+2

Here is a good tutorial for what you might want to do: http://diveintopython3.ep.io/files.html#read

'seek' is the method of finding the byte you want. The link above talks about the cautions you should take

0
source

All Articles