This tutorial will show you how to download files in Python.
The wget command is a powerful tool for downloading files from the Internet, but it’s only available on some Linux systems and there are lots of options that need exploring before getting started. Let’s take an introductory look at what we can do with this versatile tool first.,
The “python download file from url and save to directory” is a command-line tool that can be used to download files from the Internet. The wget is a very versatile tool, and it can also be used for downloading files from websites.
Having to manually download many files from the Internet as part of your daily routine might be a pain. And if you want to automate your file downloads, Python’s Wget is the program to use.
This article will teach you how to download files in a variety of methods, from using the simple Python wget command to writing a script to download numerous files at once.
Let’s get started!
This will be a hands-on presentation in this course. If you want to follow along, make sure you have the following items on hand:
Wget for Windows: Downloading and Installing
Wget is a command-line tool for downloading files from the internet. Aside from being included with Unix-based operating systems, the wget command also has a Windows counterpart. The most recent Wget Windows version is 1.21.6 at the time of writing.
Let’s go through how to download and install Wget on your Windows PC before using the wget command to get files.
1. Download Wget for Windows, either 64-bit or 32-bit.
2. Locate the wget.exe file you downloaded in File Explorer, then copy and paste it into the C:WindowsSystem32 directory to add wget.exe to the PATH environment variable. The PATH environment variable provides a collection of folders that will be searched for commands and executable applications.
You may execute the wget command from any working directory at the command prompt by include wget.exe in the PATH environment variable.
3. Now, open the command prompt and use the command below to confirm the version (—version) of Wget (wget) you downloaded.
Wget has been successfully installed on your PC if you see the output shown in the image below.
Checking to see whether Wget was installed properly.
Directly downloading a file from a URL
Let’s get started with basic wget commands now that you’ve installed Wget. You could wish to get a file from a certain URL. In such instance, all you need is the basic wget command syntax and the URL from which you want to download the file.
Download a File Using a PowerShell wget Command Alternative
The basic syntax for executing the wget command is shown below. You’ll see that you’ll enter numerous arguments following the wget command, followed by the website URL.
Adding a File to Your Working Directory
With the wget command syntax you learned still fresh in your memory, let’s look at Adding a File to Your Working Directory by running the wget without added options.
Run the command below to download the wget.exe file to the working directory from the provided URL (https://eternallybored.org/misc/wget/1.21.1/64/wget.exe).
The file has been successfully downloaded when you see this output on your command prompt.
A single file is downloaded to the working directory.
Getting a File to a Specific Location
You’ve just downloaded a file to your current working directory, but what if you want it to go to a specified file path? If that’s the case, use the command below to provide the download location.
Use the —directory-prefix option with the wget command to indicate the file path (C:TempDownloads) to store the file you’re downloading.
https://eternallybored.org/misc/wget/1.21.1/64/wget.exe wget -directory-prefix=C:TempDownloads
To ensure that you’ve successfully downloaded the file, open File Explorer and browse to the download location you chose (C:TempDownloads).
Verifying that the file was successfully downloaded
Renaming and Downloading a File
It’s cool enough that you can download a file to your favorite directory with a single command. However, it’s possible that you’d want to download a file with a different name. If that’s the case, the -o option is the solution! Using the -o switch, you may change the name of the file you’re downloading.
To download the wget.exe file from a specified URL, use the basic wget command syntax shown below. Add the -o option this time to rename the file you’re downloading. So you’re renaming the file new get.exe instead of wget.exe.
new wget.exe wget -o https://eternallybored.org/misc/wget/1.21.1/64/wget.exe
The downloaded file is entitled new wget.exe, as you can see in File Explorer below.
Using a Custom Name to View a Downloaded File
Downloading a Newer Version of a File
Perhaps you’d want to upgrade to a newer version of a file you’ve already downloaded. If that’s the case, use the —timestamp option in your wget command. Website applications are often updated, and the —timestamp option looks for the most recent version of the file at the supplied URL.
The wget program looks (—timestamp) for newer versions of the wget.exe file and downloads them to the C:TempDownloads directory.
https://eternallybored.org/misc/wget/1.21.1/64/wget.exe wget timestamp directory-prefix=C:TempDownloads
The outcome would be identical to the previous instances if the file (wget.exe) was changed from the version you supplied. If not, have a look at the screenshot below. Take note of the Not Modified section, which indicates that there is no newer version of the file you’re downloading.
Downloading a newer version of a file
Using a Username and Password to Download Files from a Website
To access or download particular files and material, most websites need a user to be signed in. Wget provides the —user and —password arguments to make this feasible. Wget uses a username and password to verify your connection request when downloading from a website using these settings.
The wget program is used to get files from websites that need your account’s login (myusername) and password (mypassword).
—ask-password=mypassword wget —user=myusername —ask-password=mypassword https://downloads.mongodb.com/compass/mongodb-compass-1.28.1-win32-x64.zip
If the command is successful, you should see something like the picture below.
Obtaining files from a website that is password-protected
Obtaining a Copy of a Web Page
Perhaps you’re attempting to save a local copy of a web page rather than a file. In such instance, you’ll use a similar command to download a file, but with a few more parameters.
Use the wget command to download the home page of the http://domain.com/ website and save it to the working directory as domain.com. The downloaded home page is kept in the domain.com subdirectory (-o).
Instead of publishing output to the terminal, the command generates a log file in the working directory.
wget -r -o log http://domain.com
The local copy of the downloaded web page and the log file where the download logs are recorded are shown below.
Viewing the Files that Have Been Downloaded and the Log File
You may also combine numerous choices without having to use arguments. Instead of writing options individually (-d -r -c), you may combine them in this way, as seen below (-drc).
wget -d -r -c wget -d -r -c wget -d # Standard option declaration: http://domain.com/ -o log wget -drc wget -drc wget -dr # Combined options: http://domain.com/ -o log
Obtaining a Complete Website
You may wish to download a whole website rather than just a single web page to observe how it is developed. To do this, set up the wget command as follows:
- Replicate (—mirror) the website (www.domain.com), making careful to download all files (-p), including scripts, pictures, and so on.
- To set a download destination (./local-dir), use the -P option.
- By adding the —convert-links option to your script, you may ensure that you only download the particular webpage. The majority of websites have pages with connections to other websites’ resources. When you download a website, you’re also downloading all of its associated webpages, which you may or may not require.
http://www.domain.com/wget —mirror -p —convert-links -P./local-dir
The file has been successfully downloaded when you see the output below.
Obtaining a Complete Website
As demonstrated below, Wget downloads all of the files that make up the whole website to the local-dir folder.
Viewing Website Files That Have Been Downloaded
The command below produces the same result as the one you just ran. The difference is that the —wait option forces a 15-second gap between web page downloads. The —limit option limits the download speed to 50 kilobits per second.
—mirror -p —convert-links -P./local-dir —wait=15 —limit-rate=50K wget —mirror -p —convert-links -P./local-dir http://www.domain.com/
Simultaneous Downloading of Files from Multiple URLs
Manually downloading files each day, as you did in the previous cases, is clearly a time-consuming operation. Wget allows you to download files from numerous URLs with a single command and just a single text file.
Sounds like a good deal? Let’s get started!
Open your preferred text editor and type in the URLs of the files you want to download, one after the other, as shown below.
Changing the download URLs in a text file
Run the command below to download the files from each of the URLs you specified in the text file.
The output of each file’s download progress is shown below.
Downloading several files from a text file’s URLs
Getting a Download to Resume After It Has Been Interrupted
You should know how to use the wget command to download files by now. However, it’s possible that your download was stopped. What would you do in this situation? Another useful feature of wget is the ability to continue a broken or stopped download.
Here’s an example of a download that was halted because your internet connection was lost. The download progress (7%) becomes stalled, but the eta continues to count up.
A Failed / Interrupted File Download is shown.
When you regain internet access, the download process will immediately restart. However, how would you continue the download if the command prompt abruptly crashed or your PC rebooted? The —continue option will undoubtedly come in handy.
To resume (—continue) an interrupted download of the wget.exe file, use the wget command below.
—continue wget https://download.techsmith.com/snagit/releases/snagit.exe
The paused download restarted at 7% when it was interrupted, as seen below (not always). You’ll also be able to check the total and remaining file sizes that need to be downloaded.
Resuming a File Download that Has Failed or Has Been Interrupted
Alternatively, you may specify how many times the wget command would attempt a failed or interrupted download.
If the download fails, add the —tries option to the wget command below, which sets 10 tries to finish the download of the wget.exe file. Interrupt the download by removing your computer from the internet as soon as you execute the program to illustrate how the —tries option works.
—tries=10 wget https://www.google.com/images/branding/googlelogo/1x/googlelogo color 272x92dp.png
The download has come to a halt, and the HTTP request is awaiting a response, as seen below.
Getting in the Way of the Download
Connect your computer to the internet again, and the download will resume immediately, as seen below. It’s clear that this is the second attempt to download the file.
Automatically retrying file downloads
Making a Python Script for File Downloading
So far, you’ve learnt how to download files by executing commands, but did you know you can also develop a script to download things for you? Let’s get started developing Python code.
1. Make a folder called “downloader.”
2. Launch VS Code, then click on the File menu —> Open Folder to open the ~downloader folder you created.
Using VS Code to Open a Folder
3. In your project directory, click the new file button to create a new Python script file called app.py, as seen below.
How to Make a Python Script File
4. Select New Terminal from the Terminal menu to start a new command-line terminal, as seen below.
Establishing a New Terminal
Virtual Environment Installation and Activation
Let’s get started constructing a virtual environment now that you have your project folder and script file. A virtual environment is a separate environment for Python projects in which all of the necessary packages are installed. This virtual environment will be activated in order for your software to run in the future.
To install the virtual environment package and build a virtual environment, run the instructions following in your VS Code terminal.
virtualenv download # Create a Virtual Environment called ‘download’ with pip install virtualenv
To activate your virtual environment, use one of the instructions below, depending on your operating system.
# Activate Virtual Environment for Unix/Mac download source download/bin/activate Scriptsactivate # Activate Windows Virtual Environment
wget Module Installation
Now that you’ve set up your virtual environment, it’s time to install the wget module. The wget module was created to give a Python developer community with an API. This module makes it easier to use and implement the wget command in Python.
You must save the packages in a requirements.txt file when creating a Python project. This file will assist you in installing the same version of the packages that were previously installed.
To install the Wget module and add it to the requirements.txt file, use the instructions following.
pip install wget # Install the wget module pip freeze > requirements.txt # Add wget to requirements.txt
Now copy and paste the following code into the app.py file you prepared in VS Code.
The code below modifies the file download output so that you can view the progress of each file download using a custom progress bar.
# wget import download # import the wget module # Make a class for downloading. downloader of classes: # Define a custom progress bar method: progressBar(self,current,total): def progressBar(self,current,total): percent (current / total * 100, current, total)) print(“Downloading: percent d percent percent [percent d / percent d] bytes” # Make a technique for downloading files. # Accepting the url as well as the file’s location. # By default, the location is set to an empty string. downloadFile(self, url, location=””): Def downloadFile(self, url, location=””): Def downloadFile(self, # Download the file with a custom progress bar (url, out = location, bar = self.progressBar) downloaderObj = downloaderObj = downloaderObj = downloaderO () downloadObj.downloadFile(“https://blog.debugeverything.com/wp-content/uploads/2021/04/python-virtualenv-project-structure.jpg”,”files”)
Finally, use the following command to launch the app.py script.
Each file’s download status is shown in percentages below, along with the file’s total and current downloaded sizes in bytes.
Using the app.py script to get files
You’ve learnt how to use the Python wget command to download files throughout this lesson. You’ve also downloaded files using wget commands, as well as using the wget module in a Python script to download several files.
Now, how would you utilize Python Wget to download files automatically in your future project? Is it possible to set up a download job that runs on a regular basis?
The “python wget (options)” is a tool that allows users to download files from the web. The options are as follows: -O, –output-file=
Frequently Asked Questions
How do I download files using wget?
A: You can use wget to download files using a URL. For example, if you want to download the file main-logo.png from https://beatsear.ch/img/main-logo.png , you would run the command wget –mirror -p img https://beatsear.ch/img/
Just remember that it is not possible to upload on Beat Saber nor PSVR without editing your script or creating custom content
Can you use wget in Python?
A: wget is a Unix utility for retrieving files from the Web. It supports http, https and ftp protocols. You can use it on any operating system that has either Python 2 or 3 installed available!
How do I download a file from Python?
- python wget download to specific directory
- wget.download python example
- wget download file
- wget python documentation
- python wget github