Kostya
Kostya

Reputation: 33

Python 3 - "file.read(x)" reads x chars, not bytes

According to documentation (https://docs.python.org/3/library/io.html?highlight=io#io.RawIOBase.read), file.read(x) should return me up to x bytes from file.

When I call file.read(1) on a UTF-8 text file, however, it returns me unicode symbols (including those which occupy 2-4 bytes).

In Python 2.7, however, everything seems to be ok. So the question is - is that a bug, or do i miss something here?

Upvotes: 0

Views: 216

Answers (1)

Silviu
Silviu

Reputation: 89

This should work

with open("myfile", "rb") as f:
    byte = f.read(1)
    while byte != b"":
        byte = f.read(1)

Upvotes: 1

Related Questions