Cross-platform issues with file's a+ mode
Michael George Lerner
mlerner at NO.umichSPAM.PLEASE.edu
Wed Apr 28 12:52:24 EDT 2004
I was helping someone debug his code today .. it worked fine on Linux, but
gave a strange error message on Windows. Here's the snippet that goes
wrong in Python 2.2 under Windows (it also happens under 2.3a1 under
Windows and Enthought's 2.3.3, so I think it's pretty generic) .. :
>>> os.remove('test.txt')
>>> f = file('test.txt','a+')
>>> f.readline()
''
>>> f.write('beep')
Traceback (most recent call last):
File "<pyshell#48>", line 1, in ?
f.write('beep')
IOError: (0, 'Error')
but .. I can make things work by seeking:
>>> f.seek(0)
>>> f.write('beep')
>>>
I don't know whether it's important or not, but if I do an f.tell() after
the readline(), it says 0L, which seems reasonable.
For comparison (and this is also true under, e.g., Linux 2.3a1),
bash-2.04$ python
Python 2.2 (#1, Feb 18 2002, 14:48:51) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.remove('test.txt')
>>> f = file('test.txt','a+')
>>> f.readline()
''
>>> f.write('beep')
>>>
What's going on here? Should I expect this behavior? I've never needed
to use a+ mode before .. is it just known to be funny or have interesting
cross-platform issues?
Thanks,
-michael
More information about the Python-list
mailing list