user3562812
user3562812

Reputation: 1829

Buffer size VS File size when reading binary file in Python

buffersize=50000
inflie = open('in.jpg','rb')
outfile = open('out.jpg','wb')

buffer = infile.read(buffersize)

while len(buffer):
    outfile.write(buffer)
    buffer = infile.read(buffersize)

I am learning basics of reading / writing binary files in python, and understand this code. I'd appreciate any help on understanding this code. Thank you!

Upvotes: 2

Views: 16215

Answers (1)

franz.fonta
franz.fonta

Reputation: 423

The documentation answers all your questions:

file.read([size])

Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). If the size argument is negative or omitted, read all data until EOF is reached. The bytes are returned as a string object. An empty string is returned when EOF is encountered immediately. (For certain files, like ttys, it makes sense to continue reading after an EOF is hit.) Note that this method may call the underlying C function fread() more than once in an effort to acquire as close to size bytes as possible. Also note that when in non-blocking mode, less data than was requested may be returned, even if no size parameter was given.

1: yes. The size parameter is interpreted as a number of bytes.

2: infile.read(50000) means "read (at most) 50000 bytes from infile". The second time you invoke this method, it will automatically read the next 50000 bytes from the file.

3: buffer is not the file but what you last read from the file. len(buffer) will evaluate to False when buffer is empty, i.e. when there's no more data to read from the file.

Upvotes: 1

Related Questions