Sometimes you may need to download file in Python and use it for further processing. Python offers several libraries such as requests, urllib, beautifulsoup, etc. for this purpose. In this article, we will look at how to download file using Python.
How to Download File Using Python
Here are the steps to download file using python using requests module. It is a very versatile module to make HTTP requests and work with responses in numerous ways. In our case, we will use it to download file using its URL.
Download file using requests module
Run the following command to install requests module.
pip install requests
In your python file, add the following lines.
#import module import requests #URL to be downloadedurl =
"https://www.python.org/static/community_logos/python-logo-master-v3-TM.png"
#download the file r = requests.get(url, allow_redirects=True) #save downloaded file open('file.png', 'wb').write(r.content)
In the above code, we basically import requests module, then use requests.get function to download file whose url we have. Finally, we write the content of downloaded file to a local file. When you run this code, it will download specified image to your present working directory. You can use this code by simply replacing the URL of file to be downloaded.
Download Large files with Requests
In the above example, r.content holds the file content in string form. But if you need to download a very large file, then it won’t be able to store all data into it at once. So we need to download large files as streams.
In this case, we modify request.get function as shown below, to include stream option
r = requests.get(url, allow_redirects=True, stream = True)
When we specify stream option as True, it will keep only the connection and response header as open, and not load the entire file for reading.
Also, instead of using r.content function we use r.iter_content to load data in chunks and specify the chunk size for streaming. Also, we loop through the downloaded file and write the new file, chunk by chunk.
with open("python.pdf","wb") as pdf: for chunk in r.iter_content(chunk_size=1024):# writing one chunk at a time to pdf file
if chunk:
pdf.write(chunk)
Here is the full code.
#import module import requests #URL to be downloadedurl =
"https://www.python.org/static/community_logos/python-logo-master-v3-TM.png"
#download the file r = requests.get(url, allow_redirects=True, stream = True) #save downloaded file with open("python.pdf","wb") as pdf: for chunk in r.iter_content(chunk_size=1024):# writing one chunk at a time to pdf file
if chunk:
pdf.write(chunk)
In this article, we have looked at two simple ways to download files in python. File download is a common requirement in web development. In fact, file download in python is usually a part of a larger application. Generally, people need to download file from another location and use it for further processing. For example, downloading a csv file, parsing it and feeding the data into a database. In such cases, the above code is very useful.
Also read:
How to Run CGI Script in Apache
How to Create Permanent Alias in Linux
How to Give User Folder Access in Linux
How to Install Swift in Ubuntu
How to Install Monit in CentOS Linux