documentation on read.encode

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Jan 18 00:47:38 EST 2018


On Wed, 17 Jan 2018 16:54:37 -0500, Larry Martell wrote:

> The code that was receiving the
> PNG was not reading and writing the file as binary. Strangely that
> worked on Linux but not on Windows.

Nothing strange about it -- on Unix and Linux systems (with the possible 
exception of Mac OS?) in Python 2 there's no difference between text and 
binary mode for ASCII-only files. In Python 2, strings are byte strings, 
not Unicode, and reading from files returns such sequences of bytes.

On Windows, reading from files in text mode treats \r\n as the end of 
line, and converts[1] such \r\n pairs to \n; it also treats ^Z byte as 
the end of file[2], or at least it used to back in the 2.5 or so days. I 
haven't tested it in more recent versions.


[1] Technically this is a build-time option, but as far as I know it is 
not just the default but pretty much universal.

[2] https://blogs.msdn.microsoft.com/oldnewthing/20040316-00/?p=40233/




-- 
Steve




More information about the Python-list mailing list