Downloading files using urllib in a for loop?

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Wed Feb 15 04:47:18 EST 2006


justsee at gmail.com wrote:
> 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]

"No such file or directory: ''" sounds to me like you are trying
to open a file called '' (empty string)

try adding some debugging

     print theimage, imagepath



>   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