Issue 3207: file.write() after file.readline() in mode "r+" (original) (raw)

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("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 "", line 1, in 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. 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
2022-04-11 14:56:35 admin set github: 47457
2008-06-26 12:41:25 amaury.forgeotdarc set status: open -> closedresolution: wont fixmessages: +
2008-06-26 11:59:59 peterdemin set messages: +
2008-06-26 11:48:36 amaury.forgeotdarc set nosy: + amaury.forgeotdarcmessages: +
2008-06-26 11:07:16 peterdemin set messages: +
2008-06-26 11:02:45 georg.brandl set assignee: georg.brandl -> messages: +
2008-06-26 10:48:29 peterdemin create