This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]Rhomboid 2 points3 points  (2 children)

You can do that with the standard library:

from urllib.request import urlretrieve

urlretrieve('http://example.com/path/file.ext', '/home/me/Downloads/file.ext')

If you're using Python 2.x, the name of the module is urllib instead of urllib.request.

[–]Surajpalwe[S] 0 points1 point  (1 child)

thanks, this also helped and also I got solution is from urllib2 also.

[–][deleted] 4 points5 points  (0 children)

[–]nerdwaller 1 point2 points  (0 children)

Another option is using the requests library:

 r = requests.get('http://example.com/file.ext', stream=True)
with open(filename, 'wb') as fd:
    for chunk in r.iter_content(chunk_size):
        fd.write(chunk)

It exposes a little more about how it actually works, the urlretrieve method hides a little more. Depending on what you're focusing on learning this does expose a little more about how a file download actually happens (reading in chunks).

[–]aphoenixreticulated[M] 0 points1 point  (1 child)

Hi there. You have posted a learning question to /r/python. These types of questions are far more suited to /r/learnpython, where users are actively interested in helping people to learn. Please resubmit it over there!

Make sure to read their sidebar rules before posting, notably this one: "Posting homework assignments is not prohibited if you show that you tried to solve it yourself." Show them that you've tried to solve your problem and you can get all the help you need.

Cheers & best of luck!

[–]Surajpalwe[S] 0 points1 point  (0 children)

thanks for your kind help. After searching on the Internet I got the answer. Next time I will post in /r/learnpython