classification
Title: file.write() after file.readline() in mode "r+"
Type: performance Stage:
Components: Documentation Versions: Python 2.5
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, georg.brandl, peterdemin
Priority: normal Keywords:

Created on 2008-06-26 10:48 by peterdemin, last changed 2008-06-26 12:41 by amaury.forgeotdarc. This issue is now closed.

Messages (6)
msg68773 - (view) Author: Peter (peterdemin) Date: 2008-06-26 10:48
Following code:
   fp = open("delete.me", "r+t")
   fp.readline()
   fp.write("New line \n")
   fp.close()

Won't do anything. I mean nor writing to file, nor raising exception. 
Nothing.
I can't find any note about this crap. So, it is the best place for it.

P.S. It's my first bug-report and I think I was wrong in filling bug-
form. Sorry.
msg68775 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-06-26 11:02
Can't reproduce on Linux on 2.6 or 3.0.
msg68776 - (view) Author: Peter (peterdemin) Date: 2008-06-26 11:07
Sorry. I use Windows XP SP2 with all updates on 26.06.2008
Python 2.5.2
msg68778 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-06-26 11:48
I tried this on windows 2000:

>>> # create a file with some text
>>> open("delete.me","w").write("some text\n")
>>>
>>> fp = open("delete.me", "r+t")
>>> fp.readline()
'some text\n'
>>> fp.write("New line \n")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 0] Error

On all 2.x versions of python I tried (2.4, 2.5.1, 2.5.2, 2.6b1, some
compiled with VS7.1, some with VS8.0)

With python3.0, there is no error, and the "New line" is appended at the
end of the file.

issue1636874 may be related to this one.
msg68779 - (view) Author: Peter (peterdemin) Date: 2008-06-26 11:59
Amaury Forgeot d'Arc, your example really raise IOError 0
Thing is that you had 1 string in the file
Here is it:
>>> open("delete.me", "w").write("first\nsecond\nthird")
>>> fp = open("delete.me", "r+t")
>>> fp.readline()
'first\n'
>>> fp.write("Newbie")
>>> fp.close()
>>> open("delete.me", "r").read()
'first\nsecond\nthird'
msg68781 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-06-26 12:41
Yes, the exact behaviour depends on multiple aspects.

You should follow the C library conventions: 
http://www.cplusplus.com/reference/clibrary/cstdio/fopen.html
"""
For the modes where both read and writing (or appending) are allowed
(those which include a "+" sign), the stream should be flushed (fflush)
or repositioned (fseek, fsetpos, rewind) between either a reading
operation followed by a writing operation or a writing operation
followed by a reading operation.
"""

In your case, I suggest a call to fp.seek(0, os.SEEK_CUR) before you
start writing data. And a fp.flush() after, in case you want to read again.

Python 3.0 has a completely new I/O implementation, which may have its
own problems, but hopefully the same on all platforms. And it happens to
do the right thing in your example.
History
Date User Action Args
2008-06-26 12:41:25amaury.forgeotdarcsetstatus: open -> closed
resolution: wont fix
messages: + msg68781
2008-06-26 11:59:59peterdeminsetmessages: + msg68779
2008-06-26 11:48:36amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg68778
2008-06-26 11:07:16peterdeminsetmessages: + msg68776
2008-06-26 11:02:45georg.brandlsetassignee: georg.brandl ->
messages: + msg68775
2008-06-26 10:48:29peterdemincreate