Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: why does peek(1) return 8K bytes instead of 1 byte?

I'm using Python 3, and the peek() method for buffered file I/O doesn't seem to work as documented. For example, the following code illustrates the problem -- it prints 8192 as the length of the byte string returned by f.peek(1):

jpg_file = 'DRM_1851.JPG'
with open(jpg_file, 'rb') as f:
    next_byte = f.peek(1)
    print(len(next_byte))

I sometimes want to peek at the next byte without moving the file pointer, but since the above doesn't work I'm doing something this in those places instead:

next_byte = f.read(1) # read a byte
f.seek(-1,1) # move the file pointer back one byte

That works, but feels like a kludge. Am I misunderstanding something about how peek() works?

like image 831
Doug Mahugh Avatar asked Aug 01 '14 00:08

Doug Mahugh


People also ask

Does Python support a peek method for its file objects?

Does Python support a peek like method for its file objects? A short answer is, "No, it does not." Peek was (I believe) put into is drastically simplified by 1-character look-ahead. Pascal was underlying I/O system.

How do I use bytes in Python?

Python bytes() The bytes() method returns a immutable bytes object initialized with the given size and data. The syntax of bytes() method is: The bytes() method returns a bytes object which is an immmutable (cannot be modified) sequence of integers in the range 0 <=x < 256. If you want to use the mutable version, use bytearray() method.

What does the bytes () method return?

The bytes () method returns a bytes object of the given size and initialization values. string = "Python is interesting." # string with encoding 'utf-8'

What is ByteArray in Python?

Bytes, Bytearray. Python supports a range of types to store sequences. There are six sequence types: strings, byte sequences (bytes objects), byte arrays (bytearray objects), lists, tuples, and range objects. Strings contain Unicode characters. Their literals are written in single or double quotes : 'python', "data".


1 Answers

From the Python docs:

peek([size])

Return bytes from the stream without advancing the position. At most one single read on the raw stream is done to satisfy the call. The number of bytes returned may be less or more than requested.

Emphasis mine.

Since the file pointer isn't moved in peek, it doesn't really matter if peek reads more than the amount you want. Just take a substring after you peek: next_byte = f.peek(1)[:1]

like image 187
Colonel Thirty Two Avatar answered Sep 21 '22 22:09

Colonel Thirty Two