Python 3 & Wget – Download Images from CSV.
Objective
I wanted to download a list of files from an S3 bucket using python 3. I used a standard CSV in UTF-8 format. Here is an example CSV. It’s one column with just the filenames.
I could have built out the full
import csv
import wget
#open import file
with open("c:\\csv\\files-to-download.csv", newline='', encoding='utf-8') as f:
#Assign the import file to the DictReader "reader"
reader = csv.DictReader(f)
#Now loop through all rows and build out variables
for row in reader:
filename = row['name']
print('Beginning file download with wget module')
url = 'http://www.example.com/images/'
wget.download(url + filename, 'c:\\csv\\images\\' + filename)
Breakdown
First, I importe
pip install wget
Next, I opened the CSV that contains the list of files I needed to download.
with open("c:\\csv\\files-to-download.csv", newline='', encoding='utf-8') as f:
After that I passed the file into a DictReader called “Reader”:
reader = csv.DictReader(f)
Now, I will loop through each row in the file
for row in reader:
On the first pass through the
filename = row['name']
Optional – Print a message that the program is about to download the file.
print('Beginning file download with wget module')
To build the full path download
url = 'http://www.example.com/images/'
Here at the end the
wget.download(url + filename, 'c:\\csv\\images\\' + filename)
Here is the code in action downloading our 4 test pictures.
</figure>