[Tutor] Download file from the web and store it locally.
Alan Gauld
alan.gauld at freenet.co.uk
Mon May 22 23:24:37 CEST 2006
> I have changed the code as following but it still printing the
> contains as something I don't know what it is instead of saving
> locally!
You need to step back a bit and get a better understanding of
variables
and functions and how to use them. You are trying to run before you
can walk.
> import urllib
> import sys
>
> # Get a file-like object from the web and store it locally.
>
> url =
> "http://www.co.rowan.nc.us/enviroservs/planning_data/countyline.zip"
>
>
> # connect to the remote
> try:
> remote = urllib.urlopen(url)
> except:
> print "Connot open URL"
> sys.exit()
At this stage you have a connection to the zip file.
> # Read from the object, storing the page's contents in 's'.
> s = f.read()
But here you are trying to read from f which doesn't exist!
> m = "D\\Temp\\file"
And here you assign a filename to m (except the filename
is invalid because it doesn't have a colon after the D)
> print remote.info()
Now this should print some info about your connection
> m = urllib.urlretrieve
Now you replace the filename with a function from the urllib module.
> print "Content:"
> print s
> print "Download Done"
You print out the contents of s which probanly are invalid
since f didn't exist.
> f.close()
And you close f which still doesn't exist.
You need to sit down and work through which variables you need
and which functions of urllib you need.
I suspect you only need to use urllib.urlretrieve() and your total
code should only be 3 or 4 lines long.
Alan G.
More information about the Tutor
mailing list