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!!!
>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