random writing access to a file in Python

Claudio Grondi claudio.grondi at freenet.de
Fri Aug 25 10:39:14 EDT 2006


Tim Peters wrote:
> [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.

Thank you much for the quick response.

The core of my problem was ... trying to use 'wb' or 'w+b' ... (stupid 
me ...)

Claudio Grondi



More information about the Python-list mailing list