Reading / writing...

Andrew Thompson andrew.thompson at ashecastle.com
Thu Jan 16 14:00:21 CET 2003


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)

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
>>> f.seek(0)
>>> f.read(3)      #this all works.
'boo'
>>> f.write('b')   #this write fails, although the file pointer moves
on...
>>> f.seek(3)
>>> f.read(1)       #we still have the original character.
'a' 
>>> f.seek(3)       #trying again, but do a direct seek() first.
>>> f.write('b')
>>> f.seek(3)
>>> f.read(1)
'b'






More information about the Python-list mailing list