title: Download files from FTP or HTTP using filenames in CSV - Python 3
body: <p>I have a csv file of products for an ecommerce site I'm working on, as well as FTP access to the corresponding images for each product (~15K products). </p>
  
  <p>I would like to use Python to pull only the images listed in the csv from either the FTP or HTTP and save them locally. </p>
  
  <pre><code>import urllib.request
  import urllib.parse
  import re
  
  url='http://www.fakesite.com/pimages/filename.jpg'
  
  split = urllib.parse.urlsplit(url)
  filename = split.path.split("/")[-1]
  urllib.request.urlretrieve(url, filename)
  
  print(filename)
  
  saveFile = open(filename,'r')
  saveFile.close()
  
  import csv
  
  with open('test.csv') as csvfile:
      readCSV = csv.reader(csvfile, delimiter=",")
  
      images = []
  
      for row in readCSV:
          image = row[14]
  
  print(image)
  </code></pre>
  
  <p>The code I have currently can pull the filename from the URL and save the file as that filename. It can also pull the filename of the image from the CSV file. (filename and image are the exact same) What I need it to do, is input the filename, from the CSV into the end of the URL, and then save that file as the filename. </p>
  
  <p>I have graduated to this: </p>
  
  <pre><code>import urllib.request
  import urllib.parse
  import re
  import os
  import csv
  
  with open('test.csv') as csvfile:
      readCSV = csv.reader(csvfile, delimiter=",")
  
      images = []
  
      for row in readCSV:
          image = row[14]
  
          images.append(image)
  
  
  x ='http://www.fakesite.com/pimages/'
  
  url = os.path.join (x,image)
  
  split = urllib.parse.urlsplit(url)
  filename = split.path.split("/")[-1]
  urllib.request.urlretrieve(url,filename)
  
  
  
  saveFile = open(filename,'r')
  saveFile.close()
  </code></pre>
  
  <p>Now this is great. It works perfectly. It is pulling the correct filename out of the CSV file, adding it on to the end of the URL, downloading the file, and saving it as the filename. </p>
  
  <p>However, I can't seem to figure out how to make this work for more than one line of the CSV file. As of now, it takes that last line, and pulls the relevant information. Ideally, I would use the CSV file with all of the products on it, and it would go through and download every single one, not just the last image. </p>