Bug? On WindowsNT, f.seek() and f.tell() aren't symmetric

Tim Peters tim_one at email.msn.com
Wed May 19 01:41:41 EDT 1999


[Oliver Steele, gets in trouble playing with f.seek and f.tell under
 Windows after opening a Unix text file in text mode]

Python uses MS's implementations of these functions.  You got in trouble
with your comment already <wink>:

    # create a two-line file
    f = open('test.txt', 'wb')
    f.write('a\nb\n')
    f.close()

That may be a two-line text file under Unix, but it's not to Windows.
Change it to

    f.write('a\r\nb\r\n', 'wb')  # now the comment tells Windows truth

and your Windows problem goes away.  Text files aren't portable, period.

> ...
> The upshot of this is that it's possible to develop a Python program
> under MacOS or UNIX, using the documentation available on those
> platforms, that fails under NT

Or vice versa -- Python lets you do *lots* of platform-dependent things.
It's not always obvious when you're doing them, but you probably won't fall
into *this* one again <wink>.

> ...
> the fix was to open the local files in 'rb' mode on Windows, and 'r'
> mode elsewhere.

Can't you open them in 'rb' mode everywhere?  That's the usual fix:  text
files are platform-dependent; binary files work the same everywhere.

learn-to-read-a-plain-"r"-as-"random"<wink>-ly y'rs  - tim






More information about the Python-list mailing list