bugs at iter file() ?

Tim Peters tim.peters at gmail.com
Thu Jul 15 04:59:07 CEST 2004

[dswj at plasa.com]
> Python 2.3.4, winxp:
> I have a large text file that unknowingly contains ascii
> character 1A, or chr(26).

Then, when you're running on Windows, and don't want chr(26) to be
treated as an end-of-file marker, you must open the file in binary

> And doing this:
> for line in file(sys.argv[1]):
>     print line
> would stop iteration at the specific line containing ascii
> char 1A, without raising exception or warning, although
> there were still remaining lines which has not been
> iterated. I was wondering whether this was a bug in
> Python.

Nope, that's how Windows treats all files opened in text mode,
regardless of programming language.  Do file(sys.argv[1], "rb")
instead to open it in binary mode.  Linux people should also open
binary files in binary mode, for portability, but since the "text
versus binary" distinction doesn't exist on Linux, many don't.

More information about the Python-list mailing list