pread/pwrite (was Re: files: direct access - NOT sequential)

Jeff Epler jepler at unpythonic.net
Tue Apr 15 12:19:37 EDT 2003


On Tue, Apr 15, 2003 at 12:33:54PM +0000, Alex Martelli wrote:
> 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).

Speaking of which, (if a patch were supplied,) is it remotely likely
that the os module would get pread/pwrite functions?  These make
lseek+read or lseek+write atomic, which is useful if two threads share
the same file descriptor and want to use random-access to it in a
threadsafe way.

(nicest would be if file.read and file.write took an optional pos=
argument, but pread/pwrite are for file-descriptors, not FILE*s so stdio
buffering would louse things up)

Jeff





More information about the Python-list mailing list