Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How does while loop know to move to the next block of info?

import urllib.request, urllib.parse, urllib.error

img = urllib.request.urlopen('http://data.pr4e.org/cover3.jpg')
fhand = open('cover3.jpg', 'wb')
size = 0
while True:
    info = img.read(100000)
    if len(info) < 1: break
    size = size + len(info)
    fhand.write(info)

print(size, 'characters copied.')
fhand.close()

How does this while loop know to move to the next block of 100000 bytes? I understand that in for loops, the loop moves forward through data. My understanding of while loops was that they do the same thing over and over unless told otherwise. I am not seeing where the loop is instructed not to read the same 100000 bytes over and over.

For reference, this is simply an example in chapter 12 in Python for Everybody.

Thanks!!!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The read() function will read the 100000 bytes and move the pointer to the end of that. So during the next read(), it will read from the 100001 byte. So it is not reading the same 100000 bytes over and over

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading