I am currently doing a course on machine learning in UDACITY . In there they have written some code in python 2.7 but as i am currently using python 3.5 , i am getting some error . This is the code
JavaScript
x
5
1
import urllib
2
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
3
urllib.urlretrieve(url, filename="../enron_mail_20150507.tgz")
4
print ("download complete!")
5
I tried urllib.request .
JavaScript
1
5
1
import urllib
2
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
3
urllib.request(url, filename="../enron_mail_20150507.tgz")
4
print ("download complete!")
5
But still gives me error .
JavaScript
1
3
1
urllib.request(url, filename="../enron_mail_20150507.tgz")
2
TypeError: 'module' object is not callable
3
I am using PyCharm as my IDE .
Advertisement
Answer
You’d use urllib.request.urlretrieve
. Note that this function “may become deprecated at some point in the future”, so you might be better off using the less likely to be deprecated interface:
JavaScript
1
11
11
1
# Adapted from the source:
2
# https://hg.python.org/cpython/file/3.5/Lib/urllib/request.py#l170
3
with open(filename, 'wb') as out_file:
4
with contextlib.closing(urllib.request.urlopen(url)) as fp:
5
block_size = 1024 * 8
6
while True:
7
block = fp.read(block_size)
8
if not block:
9
break
10
out_file.write(block)
11
For small enough files, you could just read
and write
the whole thing and drop the loop entirely.