Alternative of urllib.urlretrieve in Python 3.5How to implement the equivalent of urllib.urlretrieve in Python 3?Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?
Could a guilty Boris Johnson be used to cancel Brexit?
Can those paralyzed by the Hold Person spell be forcibly moved?
What is a simple, physical situation where complex numbers emerge naturally?
Sucuri detects malware on wordpress but I can't find the malicious code
Rotated Position of Integers
Why does my electric oven present the option of 40A and 50A breakers?
Is American Express widely accepted in France?
Are there practical reasons to NOT use a stepper motor with lead screw for the X and or Y axes?
Why were the Night's Watch required to be celibate?
How is it possible for Mordenkainen to be alive during the Curse of Strahd adventure?
Why are grass strips more dangerous than tarmac?
Is it legal in the UK for politicians to lie to the public for political gain?
Credit card offering 0.5 miles for every cent rounded up. Too good to be true?
Pros and cons of writing a book review?
Strange math syntax in old basic listing
Opposite of "Squeaky wheel gets the grease"
How to provide realism without making readers think grimdark
Is the capacitor drawn or wired wrongly?
How can a single Member of the House block a Congressional bill?
Accidentally cashed a check twice
How can Iron Man's suit withstand this?
What does War Machine's "Canopy! Canopy!" line mean in "Avengers: Endgame"?
Please help me identify this plane
Computing the differentials in the Adams spectral sequence
Alternative of urllib.urlretrieve in Python 3.5
How to implement the equivalent of urllib.urlretrieve in Python 3?Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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
import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.urlretrieve(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")
I tried urllib.request .
import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.request(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")
But still gives me error .
urllib.request(url, filename="../enron_mail_20150507.tgz")
TypeError: 'module' object is not callable
I am using PyCharm as my IDE .
python python-3.x urllib2
add a comment |
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
import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.urlretrieve(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")
I tried urllib.request .
import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.request(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")
But still gives me error .
urllib.request(url, filename="../enron_mail_20150507.tgz")
TypeError: 'module' object is not callable
I am using PyCharm as my IDE .
python python-3.x urllib2
ifrequestsis available, I'd use that
– Wayne Werner
Jul 13 '16 at 17:57
1
Someone just answerd it and worked for me . instead of urllib.urlretrive one would have to use urllib.request.urlretrieve
– CodeHead
Jul 13 '16 at 17:59
add a comment |
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
import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.urlretrieve(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")
I tried urllib.request .
import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.request(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")
But still gives me error .
urllib.request(url, filename="../enron_mail_20150507.tgz")
TypeError: 'module' object is not callable
I am using PyCharm as my IDE .
python python-3.x urllib2
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
import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.urlretrieve(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")
I tried urllib.request .
import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.request(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")
But still gives me error .
urllib.request(url, filename="../enron_mail_20150507.tgz")
TypeError: 'module' object is not callable
I am using PyCharm as my IDE .
python python-3.x urllib2
python python-3.x urllib2
asked Jul 13 '16 at 17:48
CodeHeadCodeHead
57110
57110
ifrequestsis available, I'd use that
– Wayne Werner
Jul 13 '16 at 17:57
1
Someone just answerd it and worked for me . instead of urllib.urlretrive one would have to use urllib.request.urlretrieve
– CodeHead
Jul 13 '16 at 17:59
add a comment |
ifrequestsis available, I'd use that
– Wayne Werner
Jul 13 '16 at 17:57
1
Someone just answerd it and worked for me . instead of urllib.urlretrive one would have to use urllib.request.urlretrieve
– CodeHead
Jul 13 '16 at 17:59
if
requests is available, I'd use that– Wayne Werner
Jul 13 '16 at 17:57
if
requests is available, I'd use that– Wayne Werner
Jul 13 '16 at 17:57
1
1
Someone just answerd it and worked for me . instead of urllib.urlretrive one would have to use urllib.request.urlretrieve
– CodeHead
Jul 13 '16 at 17:59
Someone just answerd it and worked for me . instead of urllib.urlretrive one would have to use urllib.request.urlretrieve
– CodeHead
Jul 13 '16 at 17:59
add a comment |
2 Answers
2
active
oldest
votes
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:
# Adapted from the source:
# https://hg.python.org/cpython/file/3.5/Lib/urllib/request.py#l170
with open(filename, 'wb') as out_file:
with contextlib.closing(urllib.request.urlopen(url)) as fp:
block_size = 1024 * 8
while True:
block = fp.read(block_size)
if not block:
break
out_file.write(block)
For small enough files, you could just read and write the whole thing and drop the loop entirely.
Thanks a lot , It worked fine .
– CodeHead
Jul 13 '16 at 18:04
add a comment |
I know this question has long been answered but I'll contribute for any future viewer.
The proposed solution is good but the main issue if that it can generate empty files if you are using invalid urls.
As a workaround to this problem here is how I adapted the code:
def getfile(url,filename,timeout=45):
with contextlib.closing(urlopen(url,timeout=timeout)) as fp:
block_size = 1024 * 8
block = fp.read(block_size)
if block:
with open(filename,'wb') as out_file:
out_file.write(block)
while True:
block = fp.read(block_size)
if not block:
break
out_file.write(block)
else:
raise Exception ('nonexisting file or connection error')
I hope this help.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38358521%2falternative-of-urllib-urlretrieve-in-python-3-5%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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:
# Adapted from the source:
# https://hg.python.org/cpython/file/3.5/Lib/urllib/request.py#l170
with open(filename, 'wb') as out_file:
with contextlib.closing(urllib.request.urlopen(url)) as fp:
block_size = 1024 * 8
while True:
block = fp.read(block_size)
if not block:
break
out_file.write(block)
For small enough files, you could just read and write the whole thing and drop the loop entirely.
Thanks a lot , It worked fine .
– CodeHead
Jul 13 '16 at 18:04
add a comment |
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:
# Adapted from the source:
# https://hg.python.org/cpython/file/3.5/Lib/urllib/request.py#l170
with open(filename, 'wb') as out_file:
with contextlib.closing(urllib.request.urlopen(url)) as fp:
block_size = 1024 * 8
while True:
block = fp.read(block_size)
if not block:
break
out_file.write(block)
For small enough files, you could just read and write the whole thing and drop the loop entirely.
Thanks a lot , It worked fine .
– CodeHead
Jul 13 '16 at 18:04
add a comment |
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:
# Adapted from the source:
# https://hg.python.org/cpython/file/3.5/Lib/urllib/request.py#l170
with open(filename, 'wb') as out_file:
with contextlib.closing(urllib.request.urlopen(url)) as fp:
block_size = 1024 * 8
while True:
block = fp.read(block_size)
if not block:
break
out_file.write(block)
For small enough files, you could just read and write the whole thing and drop the loop entirely.
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:
# Adapted from the source:
# https://hg.python.org/cpython/file/3.5/Lib/urllib/request.py#l170
with open(filename, 'wb') as out_file:
with contextlib.closing(urllib.request.urlopen(url)) as fp:
block_size = 1024 * 8
while True:
block = fp.read(block_size)
if not block:
break
out_file.write(block)
For small enough files, you could just read and write the whole thing and drop the loop entirely.
edited Jul 13 '16 at 18:00
answered Jul 13 '16 at 17:54
mgilsonmgilson
217k41429542
217k41429542
Thanks a lot , It worked fine .
– CodeHead
Jul 13 '16 at 18:04
add a comment |
Thanks a lot , It worked fine .
– CodeHead
Jul 13 '16 at 18:04
Thanks a lot , It worked fine .
– CodeHead
Jul 13 '16 at 18:04
Thanks a lot , It worked fine .
– CodeHead
Jul 13 '16 at 18:04
add a comment |
I know this question has long been answered but I'll contribute for any future viewer.
The proposed solution is good but the main issue if that it can generate empty files if you are using invalid urls.
As a workaround to this problem here is how I adapted the code:
def getfile(url,filename,timeout=45):
with contextlib.closing(urlopen(url,timeout=timeout)) as fp:
block_size = 1024 * 8
block = fp.read(block_size)
if block:
with open(filename,'wb') as out_file:
out_file.write(block)
while True:
block = fp.read(block_size)
if not block:
break
out_file.write(block)
else:
raise Exception ('nonexisting file or connection error')
I hope this help.
add a comment |
I know this question has long been answered but I'll contribute for any future viewer.
The proposed solution is good but the main issue if that it can generate empty files if you are using invalid urls.
As a workaround to this problem here is how I adapted the code:
def getfile(url,filename,timeout=45):
with contextlib.closing(urlopen(url,timeout=timeout)) as fp:
block_size = 1024 * 8
block = fp.read(block_size)
if block:
with open(filename,'wb') as out_file:
out_file.write(block)
while True:
block = fp.read(block_size)
if not block:
break
out_file.write(block)
else:
raise Exception ('nonexisting file or connection error')
I hope this help.
add a comment |
I know this question has long been answered but I'll contribute for any future viewer.
The proposed solution is good but the main issue if that it can generate empty files if you are using invalid urls.
As a workaround to this problem here is how I adapted the code:
def getfile(url,filename,timeout=45):
with contextlib.closing(urlopen(url,timeout=timeout)) as fp:
block_size = 1024 * 8
block = fp.read(block_size)
if block:
with open(filename,'wb') as out_file:
out_file.write(block)
while True:
block = fp.read(block_size)
if not block:
break
out_file.write(block)
else:
raise Exception ('nonexisting file or connection error')
I hope this help.
I know this question has long been answered but I'll contribute for any future viewer.
The proposed solution is good but the main issue if that it can generate empty files if you are using invalid urls.
As a workaround to this problem here is how I adapted the code:
def getfile(url,filename,timeout=45):
with contextlib.closing(urlopen(url,timeout=timeout)) as fp:
block_size = 1024 * 8
block = fp.read(block_size)
if block:
with open(filename,'wb') as out_file:
out_file.write(block)
while True:
block = fp.read(block_size)
if not block:
break
out_file.write(block)
else:
raise Exception ('nonexisting file or connection error')
I hope this help.
answered Mar 24 at 11:52
Al rlAl rl
195
195
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38358521%2falternative-of-urllib-urlretrieve-in-python-3-5%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
if
requestsis available, I'd use that– Wayne Werner
Jul 13 '16 at 17:57
1
Someone just answerd it and worked for me . instead of urllib.urlretrive one would have to use urllib.request.urlretrieve
– CodeHead
Jul 13 '16 at 17:59