files: direct access - NOT sequential

Alex Martelli aleax at aleax.it
Tue Apr 15 14:33:54 CEST 2003


Patrick Carabin wrote:

> i search a means to get " direct access " inside files, eg mixing
> read & write operaions for the same file
> example:
> given a file with contents : "qwerty" , positionning on the "w", and
> writing a "N" , so the file becomes "qNerty"
> How can i achieve this in Python ?

thefile = open('thefile', 'r+')    # must be already-existing
thefile.seek(1)
thefile.write('N')
thefile.close()

Here, in fact, we're NOT mixing read and write operations -- the
only _operation_ is a write -- but, if we wanted to, we might (that
is what mode 'r+' means).


Alex





More information about the Python-list mailing list