Basic http file downloading and saving to disk in python? Basic http file downloading and saving to disk in python? python python

Basic http file downloading and saving to disk in python?


A clean way to download a file is:

import urllibtestfile = urllib.URLopener()testfile.retrieve("http://randomsite.com/file.gz", "file.gz")

This downloads a file from a website and names it file.gz. This is one of my favorite solutions, from Downloading a picture via urllib and python.

This example uses the urllib library, and it will directly retrieve the file form a source.


As mentioned here:

import urlliburllib.urlretrieve ("http://randomsite.com/file.gz", "file.gz")

EDIT: If you still want to use requests, take a look at this question or this one.


For Python3+ URLopener is deprecated.And when used you will get error as below:

url_opener = urllib.URLopener() AttributeError: module 'urllib' has no attribute 'URLopener'

So, try:

import urllib.request urllib.request.urlretrieve(url, filename)