469,631 Members | 1,526 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

File read-write mode: problem appending after reading

Hi all,

Working with read and write operations on a file I stumbled on a
complication when writes fail following a read to the end.
>>f = file ('T:/z', 'r+b')
f.write ('abcdefg')
f.tell ()
>>f.seek (0)
f.read ()
>>f.flush () # Calling or not makes no difference
f.write ('abcdefg')
Traceback (most recent call last):
File "<pyshell#62>", line 1, in -toplevel-
f.write ('abcdefg')
IOError: (0, 'Error')

Flushing doesn't help. I found two work arounds:
>>f.read ()
>>f.read () # Workaround 1: A second read (returning an empty string)
>>f.write ('abcdefg')
(No error)
>>f.read ()
>>f.seek (f.tell ()) # Workaround 2: Setting the cursor (to where
it is!)
>>f.write ('abcdefg')
(No error)

I found no problem with writing into the file. So it looks like it has
to do with the cursor which a read puts past the end, unless it is past
the end, in which case it goes back to the end. Is there a less kludgy
alternative to "fseek (ftell ())"?


Oct 13 '06 #1
0 900

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Yang Li Ke | last post: by
6 posts views Thread by Russell E. Owen | last post: by
3 posts views Thread by Tony Murphy | last post: by
4 posts views Thread by yo_mismo | last post: by
4 posts views Thread by John please don't spam me! | last post: by
5 posts views Thread by lovecreatesbea... | last post: by
15 posts views Thread by lxyone | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.