iterating over the lines of a file - difference between Python 2.7 and 3?

Peter Otten __peter__ at
Thu Jan 17 18:59:46 CET 2013

Wolfgang Maier wrote:

> What will my IO object return then when I read from it in Python 2.7? str
> where Python3 gives bytes, and unicode instead of str ? This is what I
> understood from the Python 2.7 io module doc.

You can always double-check in the interpreter:
>>> with open("tmp.txt", "w") as f: f.write("let's try\n")
>>> import io
>>> with"tmp.txt", "r") as f: print type(
<type 'unicode'>
>>> with"tmp.txt", "rb") as f: print type(
<type 'str'>

So yes. Also:

>>> str is bytes

More information about the Python-list mailing list