Downloading files using urllib in a for loop?

justsee at gmail.com justsee at gmail.com
Wed Feb 15 04:36:56 EST 2006


Hi,
 I'm using Python 2.3 on Windows for the first time, and am doing
something wrong in using urllib to retrieve images from urls embedded
in a csv file. If I explicitly specify a url and image name it works
fine(commented example in the code), but if I pass in variables in this
for loop it throws errors:

--- The script:

import csv, urllib
reader = csv.reader(open("source.csv"))
for x,y,z,imagepath in reader
  theurl = imagepath[:55]
  theimage = imagepath[55:-8]
  urllib.urlretrieve(theurl, theimage)
  #urllib.urlretrieve("http://someurl/image.gif", "image.gif") # works!

--- The errors:

This throws the following errors:
  File "getimages.py", line 9, in ?
    urllib.urlretrieve(theurl,theimage)
  File "C:\Python23\lib\urllib.py", line 83, in urlretrieve
    return _urlopener.retrieve(url, filename, reporthook, data)
  File "C:\Python23\lib\urllib.py", line 213, in retrieve
    fp = self.open(url, data)
  File "C:\Python23\lib\urllib.py", line 181, in open
    return getattr(self, name)(url)
  File "C:\Python23\lib\urllib.py", line 410, in open_file
    return self.open_local_file(url)
  File "C:\Python23\lib\urllib.py", line 420, in open_local_file
    raise IOError(e.errno, e.strerror, e.filename)
IOError: [Errno 2] No such file or directory: ''

---

Would really appreciate some pointers on the right way to loop through
and retrieve images, as I've tried various other solutions but am
clearly missing something simple!

Thanks,

justin.




More information about the Python-list mailing list