← Go Back

How to Download a File via HTTP

Using the Requests third-party library (to install: pip install requests):

import requests

# File URL.
url = "https://www.python.org/static/community_logos/python-logo-master-v3-TM.png"
with requests.get(url) as r:
# Name with which you want to download the file.
with open("python-logo.png", "wb") as f:
f.write(r.content)

Using the standard library:

from urllib.request import urlopen

# File URL.
url = "https://www.python.org/static/community_logos/python-logo-master-v3-TM.png"
r = urlopen(url)
# Name with which you want to download the file.
with open("python-logo.png", "wb") as f:
f.write(r.read())
r.close()


http requests files


🐍 You might also find interesting: