What will I get when reading from a file? (was: Pyhon 2.x or 3.x, which is faster?)

Ben Finney ben+python at benfinney.id.au
Mon Mar 7 19:09:05 EST 2016


BartC <bc at freeuk.com> writes:

> I'm using [a third-party library function] because this kind of file
> reading in Python is a mess. If I do a read, will I get a string, a
> byte sequence object, a byte-array, or array-array, or what?

That's a strange statement; there is virtually no mystery in what you'll
get from a Python 3 file object, because you're eessentially forced to
declare explicitly what kind of results you want from it.

In fact, this no-mystery is fairly special to Python. Most other
languages will guess aspects of the file and won't force the programmer
to be explicit; if anything, newcomers are frustrated at how *little*
guesswork (i.e. how much explicit declaration) is involved in opening a
Python 3 file.

If you want bytes, you open the file in that mode. If you want text, you
must instead open it with that mode and tell it what text codec to use.

And so on. Where are you seeing mysterious results?

-- 
 \      “Any intelligent fool can make things bigger and more complex… |
  `\    It takes a touch of genius – and a lot of courage – to move in |
_o__)                        the opposite direction.” —Albert Einstein |
Ben Finney




More information about the Python-list mailing list