Trouble with file.seek/file.tell on Win32?

Prabhu Ramachandran prabhu at aero.iitm.ernet.in
Sun Aug 15 14:54:16 EDT 2004


Hi,

I noticed peculiar behavior under Python-2.3.4 under Win32.  When I
run something like this:

f = open('t.txt', 'wb')
f.write('1\012'+'2\012'+'3\012')
f.close()
f = open('t.txt', 'r')
f.readline()
pos = f.tell()
val = f.read(1)
f.seek(pos)
assert val == f.read(1)

I get an assertion error at this point.  Everything works fine if I
read the file with 'rb' instead of 'r'.  But I can almost swear that
this used to work earlier.  I suspect that this behavior arises due to
recent changes in fileobject.c (specifically, v2.187) where universal
newline support was removed.

http://cvs.sf.net/viewcvs.py/python/python/dist/src/Objects/fileobject.c?rev=2.192&view=log

Could someone please clarify if this is this a bug or not?  I would
like to think that using tell and seek should work reliably enough for
text files.

The reason this is important for me is that I've been generating text
files (with gay abandon) for MayaVi (http://mayavi.sf.net) for three
years now.  I always open the file using 'r'/'w' and not 'rb'/'wb'.
Everything used to work fine.  It never mattered where and how exactly
the files were generated.  Now, I get strange errors since I can no
longer seek into these files reliably.  I could switch to using
'rb'/'wb' universally but all existing files will no longer work
without either requiring a fair bit of effort on my part or on the
part of users changing their saved files.  I am loathe to ask every
user to change all their files and change the newlines.

Thanks.

cheers,
prabhu

p.s. Please CC me in on replies.  I'm not tracking c.l.py.  Thanks.



More information about the Python-list mailing list