This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: file.write() after file.readline() in mode "r+"
Type: performance Stage:
Components: Documentation Versions: Python 2.5
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 2022-04-11 14:56 by admin. This issue is now closed.

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

Won't do anything. I mean nor writing to file, nor raising exception. 
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("","w").write("some text\n")
>>> fp = open("", "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("", "w").write("first\nsecond\nthird")
>>> fp = open("", "r+t")
>>> fp.readline()
>>> fp.write("Newbie")
>>> fp.close()
>>> open("", "r").read()
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:
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, 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.
Date User Action Args
2022-04-11 14:56:35adminsetgithub: 47457
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