Checkpoint 13.6.1.
- True
- Correct! The 'wb' argument stands for Write Binary.
- False
- Try again!
Q-2: True or False? The
wb
argument opens a binary file for writing only.urllib
urllib
.read
to download the entire contents of the document into a string variable (img
) then write that information to a local file as follows:import urllib.request, urllib.parse, urllib.error
img = urllib.request.urlopen('http://data.pr4e.org/cover3.jpg').read()
fhand = open('cover3.jpg', 'wb')
fhand.write(img)
fhand.close()
img
in the main memory of your computer, then opens the file cover.jpg
and writes the data out to your disk. The wb
argument for open()
opens a binary file for writing only. This program will work if the size of the file is less than the size of the memory of your computer.wb
argument opens a binary file for writing only.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()
cover.jpg
file before retrieving the next 100,000 characters of data from the web.info = img.read(100000)
in the following code?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)