Command Line File Downloader in Python


Python is a powerful programming language that provides many tools and libraries for different applications. We can also create a command line file downloader in python.

Command line downloader is used to download files from internet manually through command line interface or your terminal without using browser.

To create a command line file downloader in python we need two libraries, they are argparse and required. We should have basic knowledge of terminal or command line interface and python language before we start building this application.

Installation

Before we continue, we need to have the above mentioned libraries in our system to be installed. make sure that the libraries mentioned above are installed before we start building the application. If downloaded you can continue from step 2 else follow from step 1, we can download and install them using pip, the python package manager using the following commands.

pip install argparse
pip install requests

Step 1: Importing the Required Libraries

First of all, we need to create a new python file and import the above mentioned 2 libraries argparse and requests. We can import them by the below code −

import requests
import argparse

The argparse library is used to handle the arguments that will be given to the function through the command line interface and the requests library will be used to download the required files from internet.

Step 2: Defining the Download File Function

In the second step we will define a download_file() function which will take 2 arguments. The first is the URL of the file which we have to download and the second the name by which we have to save the file.

def download_file(url, filename):
   response = requests.get(url)
   with open(filename, "wb") as f:
      f.write(response.content)

let’s see what this does. We are using the requests library to download the file from URL provided as the argument and this will save the file with the name given as argument. The with statement will be responsible for closing the file after the file is downloaded properly.

Step 3: Parsing Command Line Arguments

The user will give the arguments with the command itself so we have to use the argparse library. This will take the arguments from the user through the command line directly instead of passing it as arguments for the function.

parser = argparse.ArgumentParser()
parser.add_argument("--url", help="provide URL", required=True)
parser.add_argument("--filename", help="Give file name", required=True)
args = parser.parse_args()

The “–url” argument is required and specifies the URL of the file to download. The “— filename” argument is also required and specifies the filename to save the downloaded file as.

Step 4: Downloading the File

After parsing the command line arguments to the, we can use the download_file function to download the file from the URL given to as arguments also save it by the file name given as argument.

download_file(args.url, args.filename)
print("File downloaded successfully.")

This code will call the download file function and download the file from the given URL as argument and print “File downloaded successfully” in the terminal after the file is downloaded.

Example

Here is the complete code for a command line file downloader in Python −

import requests
import argparse
def download_file(url, filename):
   response = requests.get(url)
   with open(filename, "wb") as f:
      f.write(response.content)
parser = argparse.ArgumentParser()
parser.add_argument("--url", help="provide URL", required=True)
parser.add_argument("--filename", help="Give file name", required=True)
args = parser.parse_args()
download_file(args.url, args.filename)
print("File downloaded successfully.")

To use this file downloader, simply run it from the command line and specify the URL and filename −

python file_downloader.py --url  https://example.com/file.txt --filename file.txt

This command, when is runned or executed, will down load the document from the URL supplied after which additionally store the document as document.txt withinside the same directory in which the command was run.

Conclusion

In this article we discussed how we can build a command line file downloader in python. We have used 2 libraries argparse and requests to make this command line file downloader. By this application we can download any file from the internet through command line interface without browser. We understood all the steps needed to make the command line file downloader. This code can be customized to fit specific needs like adding a progress bar or error handling.

Updated on: 20-Apr-2023

336 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements