Python: Download File from URL & Save

A Python can be used to download a text or a binary data from a URL by reading the response of a urllib.request.urlopen.

The downloaded data can be stored as a variable and/or saved to a local drive as a file.

Below you will find the examples of the Python code snippets for downloading the different types of files from URLs and storing them as variables or saving locally.

Cool Tip: How to set the ‘User-Agent’ HTTP request header in Python! Read More →

Download a File from URL using Python

Text Data

Use the following Python snippet to download a web page or a text file from the URL, save its content to a variable and then print it:

from urllib.request import urlopen

url = "https://www.shellhacks.com/file.csv"

# Download from URL
with urlopen(url) as file:
    content = file.read().decode()

print(content)

Read & Decode: The .read() first downloads the data in a binary format, then the .decode() converts it to a string.

If you need to save the downloaded data as a text file, you can do this as follows:

from urllib.request import urlopen

url = "https://www.shellhacks.com/file.csv"
save_as = "file.csv"

# Download from URL
with urlopen(url) as file:
    content = file.read().decode()

# Save to file
with open(save_as, 'w') as download:
    download.write(content)

Binary Data

To download a binary file (e.g. pdf, mp3, zip) from the URL and save it to your local drive, use the Python’s code as follows:

from urllib.request import urlopen

url = "https://www.shellhacks.com/file.pdf"
save_as = "file.pdf"

# Download from URL
with urlopen(url) as file:
    content = file.read()

# Save to file
with open(save_as, 'wb') as download:
    download.write(content)

The 'wb' means, that you are opening the file for writing purposes in a binary format.

If you don’t choose the binary mode you’ll get this error:

Traceback (most recent call last):
  File "python-file-downloader.py", line 12, in <module>
    download.write(content)
TypeError: write() argument must be str, not bytes

Leave a Reply