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


I am using Python 3, and the buffer file does not work as a peak () method for I / O Documents are doing For example, the following code shows the problem - this is the 8192 as the length of the byte string returned by f.peek (1) :

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

I sometimes want to peek at the next byte without moving the file pointer, but since then I have been doing something on those places instead:

 < Code> next_byte = f.read (1) #by a Read the F. Sec (-1,1) #File pointer to get a byte back  

This works, but looks like a blackspear how I work () works Is there some misunderstanding?

to:

peek ([shape])

Return the bytes from the stream without moving the position, to satisfy the call, the single single is read on the raw stream. Number of bytes returned may be more or less than the request.

Emphasis mine

Since the file does not move in the indicator peeking , it does not really matter if peeking read more than the amount you want Take a substrings after bending: next_byte = f.peek (1) [: 1]


Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -