file position *tell()* works different

Eric Brunel eric.brunel at pragmadev.N0SP4M.com
Fri Sep 19 06:49:47 EDT 2003


M-a-S wrote:
> I'm not sure if that't the reason, but the binary mode for reading is 'rb'.
> Actually, the order of 'r' and 'b' shouldn't matter. But the '+' has a different
> meaning: the file should allow "opposite" access as well, e.g. 'r+', 'rb+'
> means that you can write to the file too, while 'w+' means: open it for
> writing but permit reading too. You can try to say 'rt' for the read/text mode.

Sorry, but no you can't: the default is to open the file in text mode, and you 
can change it with a 'b', but 't' has no meaning at all. BTW, 'b' also has no 
meaning at all on all Unices: the so-called "binary" or "text" mode are the 
same, i.e. what is read is what is in the file. Windows needs it only because of 
its superfluous \r's at the end of each line.

> Anyway, you program works under Windows XP/Python 2.3 as expected:
> 
> Microsoft Windows XP [Version 5.1.2600]
> (C) Copyright 1985-2001 Microsoft Corp.
> 
> C:\Home\Programming\Python\2>py
> Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> 
>>>>^Z
>>>
> 
> C:\Home\Programming\Python\2>test.py
> Filepointer:   0
> Filepointer:  12
> Filepointer:  24
> Filepointer:  35
> Filepointer:  54
> 
> Filepointer:   0
> Filepointer:  12
> Filepointer:  24
> Filepointer:  35
> Filepointer:  54
> 
> I'm sorry if it doesn't help. The bug must be somewhere else then.
> 
> M-a-S
> 
> 
> "Peter Abel" <p-abel at t-online.de> wrote in message news:13a533e8.0309190137.2df0cc60 at posting.google.com...
> 
>>Hi all,
>>I'm working under W2k with
                     ===
>>Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32

I do confirm the methods tell and/or seek are broken on Win2K when you open the 
file in text mode: doing myFile.seek(myFile.tell()) is not a no-op. The "ghost" 
\r's at the end of lines seem to be taken into account by one of the methods and 
not by the other one. The problem also happens on Win98.

I don't know if it's a Python bug or a bug in the underlying C API. Knowing 
Windows, and considering the Python wrapper must be quite trivial, I'd bet on 
the C API...

The only workaround I found was to always open the files in binary mode, and 
explicitely ignore the \r's.

HTH
-- 
- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com





More information about the Python-list mailing list