Reading / writing...

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Thu Jan 16 14:14:38 CET 2003


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


> 
> 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
> >>> 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'
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 





More information about the Python-list mailing list