random writing access to a file in Python

Tim Peters tim.peters at gmail.com
Fri Aug 25 10:15:39 EDT 2006


[Claudio Grondi]
> I have a 250 Gbyte file (occupies the whole hard drive space)

Then where is Python stored ;-)?

> and want to change only eight bytes in this file at a given offset of appr. 200
> Gbyte (all other data in that file should remain unchanged).
>
> How can I do that in Python?

Same way you'd do it in C, really:

f = open(PATH_TO_FILE, "r+b")
f.seek(appr. 200 Gbyte)
f.write(A_STRING_CONTAINING_YOUR_NEW_8_BYTES)
f.close()

This depends on your operating system and file system and platform C
library supporting seeks to very large offsets.  For example, Windows
using NTFS does.  Try it.  Python should complain (raise an exception
during the seek() attempt) if your box refuses to cooperate.  Use as
recent a released version of Python as you can.



More information about the Python-list mailing list