470,613 Members | 2,371 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

writing some lines after reading goes wrong on windows?

Hi all
I can not write anything if I have read something.
the code as:
Python 2.4.3 (#69, Apr 11 2006, 15:32:42) [MSC v.1310 32 bit (Intel)]
on win32
IDLE 1.1.3
a=open('d:\\a','r+')
a <open file 'd:\a', mode 'r+' at 0x00A147B8> a.read() '11\n22\n33\n' a.seek(0)
a.read(1) '1' a.write("a")
a.seek(0)
a.read() '11\n22\n33\n' # it fails to write after reading
a.seek(0)
a.write("a")
a.seek(0)
a.read() 'a1\n22\n33\n' # it writes fine at the beginning of
the file
it fails to write "a". but on my debian box, it works fine.
the code on debian as:
Python 2.3.5 (#2, Sep 4 2005, 22:01:42)
[GCC 3.3.5 (Debian 1:3.3.5-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information. a=open('/a','r+')
a <open file '/a', mode 'r+' at 0x4019dd60> a.read() '11\n22\n33\n' a.seek(0)
a.read(1) '1' a.write("a")
a.seek(0)
a.read() '1a\n22\n33\n'


What's wrong here? is this a bug on windows platform.

Apr 27 '06 #1
1 933
If you are opening a file for read and write access use "rb+" or "wb+".
Seeking and subsequent read/write will then work fine. Without the "b",
Windows opens the file in its "text" mode. Don't seek on files opened
in "text" mode.

Apr 27 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Raaijmakers, Vincent (IndSys, GE Interlogix) | last post: by
6 posts views Thread by Kevin T. Ryan | last post: by
2 posts views Thread by dave | last post: by
4 posts views Thread by John Pote | last post: by
24 posts views Thread by rudranee | last post: by
5 posts views Thread by grinder | last post: by
3 posts views Thread by katz911 | last post: by
118 posts views Thread by Chuck Cheeze | last post: by
7 posts views Thread by tshad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.