Reading / writing...

Andrew Thompson andrew.thompson at
Thu Jan 16 14:37:47 CET 2003

Exactly, but to quote the Python Essential Reference (2000) guide,

'when a file is opened for update, you can perform both input and
output, as long as all output operations flush their data before any
subsequent input operations'

What is happening here is that the write() simply doesn't write,
(although the file pointer moves on)and no error condition is raised...
(how might a user know to perform a seek() to effect the write() --
without having extra-Pythonic knowledge ?)

Shouldn't Python be able to hide this from its users ?

-----Original Message-----
From: python-list-admin at [mailto:python-list-admin at]
On Behalf Of Chris Gonnerman
Sent: 16 January 2003 13:15
To: python-list at
Subject: Re: Reading / writing...

----- Original Message ----- 
From: "Andrew Thompson" <andrew.thompson at>

> Has anyone experienced a file.write() operation not actually writing 
> anything to a file, although the file pointer moves on... (I was 
> comparing prior and post seek() values to check how much data was
> written)

This is documented, normal behavior... for all Unix/Linux systems I am
familiar with, for files opened in read/write mode you MUST 
seek() before switching from read() to write() or vice-versa.

For Windoze... dunno.  The seek() isn't going to hurt anything (seek
from "here" for an offset of 0).

> In the example below, the write() operation works fine if it is 
> preceded by a seek(), but fails if it is preceded by a read().
> My original thought was that the new data simply wasn't yet flush()ed 
> into the file, but no amount of flush()ing can make the new data 
> arrive anywhere in the file.
> I am aware of the importance of flush() on all reads() after writes() 
> , but cannot find anything in the literature the other way around.
> Can anyone help?
> Andrew
> >>> f=open('test','wb+')
> >>> f.write('boo')     #write a simple 3 char string.
> >>> f.tell()
> 3L
> >>> f.write('a')       #write a further character
> >>>
> >>>      #this all works.
> 'boo'
> >>> f.write('b')   #this write fails, although the file pointer moves
> on...
> >>>
> >>>       #we still have the original character.
> 'a'
> >>>       #trying again, but do a direct seek() first.
> >>> f.write('b')
> >>>
> >>>
> 'b'
> --


More information about the Python-list mailing list