472,353 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Downloading files using urllib in a for loop?

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.

Feb 15 '06 #1
6 5643

ju*****@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.


Feb 15 '06 #2
Thanks - but have printed and verified they are valid paths and
filenames. One correction to the code I listed:
theurl = imagepath[:-8]

For some reason the values aren't being passed through
urllib.urlretrieve properly but this makes no sense to me?

Feb 15 '06 #3
Martin Franklin wrote:
"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


or, better:

print repr(theimage), repr(imagepath)

</F>

Feb 15 '06 #4
Martin Franklin wrote:
ju*****@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
I just noticed the code you sent will not work... notice the lack of a
colon ( : ) and the end of the 'for' line....

please post an exact copy of your code and also the results with the
included print debugging line (with or without repr ;) )

Cheers
Martin

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.


Feb 15 '06 #5
ju*****@gmail.com wrote:
Thanks - but have printed and verified they are valid paths and
filenames. One correction to the code I listed:
theurl = imagepath[:-8]

For some reason the values aren't being passed through
urllib.urlretrieve properly but this makes no sense to me?


A working (for me!) example:-
import urllib

paths = ["http://www.python.org/index.html", ]

for remotepath in paths:
# keep only last 10 chars (index.html)
# for local file name
localpath = remotepath[-10:]

print remotepath, localpath

urllib.urlretrieve(remotepath, localpath)
Feb 15 '06 #6
ah - thanks for your help, but what is happening is the first line
being returned contains the field names from the csv file! Schoolboy
errors :-)

Feb 15 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Blaktyger | last post by:
I would like to download some mp3 files from a web site. There is to much of them and I had the idea of writing a script to do it for me. Code:...
1
by: NewFilmFan | last post by:
I use Python 2.3 on Windows XP. I wrote this program: import httplib conn = httplib.HTTPConnection("www.x.net") conn.request("GET", "/x/y.jpg")...
1
by: Oyvind Ostlund | last post by:
Hello, I have to download a lot of files. And at the moment i am trying with URLLib. But sometimes it doesn't download the whole file. It looks...
1
by: Greg L. | last post by:
I apologize if this is not the correct group.... Using the system.net namespace I want to download an entire directory's worth of files. I've...
10
by: mwt | last post by:
This code works fine to download files from the web and write them to the local drive: import urllib f = ...
4
by: aldonnelley | last post by:
Hi there: a bit of a left-field question, I think. I'm writing a program that analyses image files downloaded with a basic crawler, and it's slow,...
9
by: Frank Potter | last post by:
I want to find a multithreaded downloading lib in python, can someone recommend one for me, please? Thanks~
7
by: Ehsan | last post by:
I foundd this code in ASPN Python Cookbook for downloading files in python but when it finished downloading files the files became corrupted and...
7
by: Jetus | last post by:
I am able to download this page (enclosed code), but I then want to download a pdf file that I can view in a regular browser by clicking on the...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.