I have a CSV file with 6 columns and many rows. I would like to download all the png or jpg from the column 'link' in a folder with the same name of my CSV file.Then I would like to rename these images with each 'title' content.
url1.png by name1.png for each files and until the last row..
I started something with this -
import csv
with open('name.csv') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in reader:
fileurl = row[0]
filename = row[1]
urllib.request.urlretrieve(fileurl, "name" filename)
Rows example -
Still learning.. Any help or suggestions to do this?
Many thanks.
CodePudding user response:
If I understand you correctly, you would like to download the file in the link
column using the title
column to form the filename.
This can be done as follows:
import urllib.request
import csv
import os
with open('name.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
name, ext = os.path.splitext(row['link'])
title_filename = f"{row['title']}{ext}".replace('/', '-')
urllib.request.urlretrieve(row['link'], title_filename)
You can use .os.path.splitext()
to split out the extension of the filename. This can then be used to combine with the entry from title
to form a new filename.
For example:
https://url.com/folder/url1.png
would save as name1.png
To deal with multiple identical title
entries, you could investigate Python's Counter()
to keep track of how many of each title you have. For example:
from collections import Counter
import urllib.request
import csv
import os
with open('name.csv') as csvfile:
reader = csv.DictReader(csvfile)
title_counts = Counter()
for row in reader:
name, ext = os.path.splitext(row['link'])
title = row['title']
title_counts[title] = 1
title_filename = f"{title}_{title_counts[title]}{ext}"
urllib.request.urlretrieve(row['link'], title_filename)