I am trying to download the images which are available as url in csv file into specific folders (named as Male, Female for classification) I created in my C:/Desktop/Images. But when I run the below code, nothing is getting downloaded and getting saved into the specific folders based on the category as present in my csv file. The contents in my csv are as in below format. It has several thousands of rows. I am trying to iterate and save the specific gender image to that particular folder created above
Format:
Male profilename https://pbs.twimg.com/profile_images/414342229096808449/fYvzqXN7_normal.png
**Code:**
import urllib.request
import urllib.error
import sys
filename = "images"
with open("{0}.csv".format(filename), 'r') as csvfile:
i = 0
for line in csvfile:
splitted_line = line.split(',')
try:
if ((splitted_line[2] != "\n") and (splitted_line[2] != '') and (splitted_line[0] == male)):
fullfilename_1 = os.path.join('C:/Desktop/Images/Male', splitted_line[1])
urllib.request.urlretrieve(splitted_line[2], fullfilename_1 + ".png")
print ("Image saved for {0} in {1} ".format(splitted_line[1],'C:/Desktop/Images/Male'))
i += 1
except:
print ("No result for {0}".format(splitted_line[1]))
try:
if ((splitted_line[2] != "\n") and (splitted_line[2] != '') and (splitted_line[0] == female)):
fullfilename_1 = os.path.join('C:/Desktop/Images/Female', splitted_line[1])
urllib.request.urlretrieve(splitted_line[2], fullfilename_1 + ".png")
print ("Image saved for {0} in {1} ".format(splitted_line[1],'C:/Desktop/Images/Female'))
i += 1
except:
print ("No result for {0}".format(splitted_line[1]))
How would I be able to download/save to the specific folder as mentioned? Is there any issue with my path not being properly mentioned? Any help would kindly be appreciated!