Use the wget Command in Python

Use the wget Command in Python

Lakshay Kapoor Oct-22, 2021 Jul-24, 2021 Python Python Wget

wget is a URL network downloader that can work in the background, and it helps in downloading files directly from the main server. In Python, this task is done by using the wget module.

the wget Module in Python

In Python’s wget module, the file’s final destination is not necessary to open in the background to download a particular file. The URL and a definite path of the file have to be mentioned to download a specific file.

To download the wget module, run the following command.

pip install wget

The download method of the wget module downloads any file in a single line.

Let us have an example to see the use of the download method of the wget module to download a file from a website. We have to download a database file known as main_database.csv, which is in the form of a CSV file from a website called www.randomdatabase.com.

import wget

site_url = 'http://www.randomdatabase.com/database_files/csv/main_database.csv'
file_name = wget.download(site_url)
print(file_name)

Note that the URL and the file’s proper path are stored in a separate variable, and that variable is the argument of the download method of the wget module.