Need script to download file at known address

R

Radioactive Man

I am fairly new to the latest verion of Python and using it on windows
95, 2000, and/or XP. What libraries, modules, functions, etc. would I
need to set up a Python script to download a file, say
"htttp://www.sound.com/files/bob.wav" to my own hard drive at
"c:\sound\bob.wav"?

I haven't found any good examples of such an operation in the
documentation at the Python website. Any suggestions are appreciated.
Thanks.
 
P

Pierre Fortin

I am fairly new to the latest verion of Python and using it on windows
95, 2000, and/or XP. What libraries, modules, functions, etc. would I
need to set up a Python script to download a file, say
"htttp://www.sound.com/files/bob.wav" to my own hard drive at
"c:\sound\bob.wav"?

I haven't found any good examples of such an operation in the
documentation at the Python website. Any suggestions are appreciated.
Thanks.


See: 11. Internet Protocols and Support
at http://python.org/doc/2.3.4/lib/lib.html

In this example, Yahoo uses 00=Jan, so 08=Sep...
['Date,Open,High,Low,Close,Volume,Adj. Close*\n',
'14-Sep-04,86.60,86.88,86.15,86.72,3953500,86.72\n', '<!--
ichart9.finance.dcn.yahoo.com uncompressed Tue Sep 14 17:55:33 PDT 2004
-->\n']
 
A

Alex Martelli

Radioactive Man said:
I am fairly new to the latest verion of Python and using it on windows
95, 2000, and/or XP. What libraries, modules, functions, etc. would I
need to set up a Python script to download a file, say
"htttp://www.sound.com/files/bob.wav" to my own hard drive at
"c:\sound\bob.wav"?

Something like:

import urllib
urllib.urlretrieve( "htttp://www.sound.com/files/bob.wav",
'c:/sound/bob.wav')

should be fine.
I haven't found any good examples of such an operation in the
documentation at the Python website. Any suggestions are appreciated.

http://docs.python.org/lib/module-urllib.html has the docs of
urlretrieve, and it's SO simple that I don't really see what "good
examples" one might provide. A book that by design is full of examples
(hopefully good) is the Python Cookbook, which in the intro to Chapter
10 (Network Programming) gives a slightly richer example which downloads
several files with urllib.urlretrieve.
http://www.python9.org/p9-zadka.ppt may have some useful pointers,
perhaps.

http://www.experts-exchange.com/Programming/Programming_Languages/Python
/Q_21048439.html has exactly the same question you asked, but you have
to "sign up" to see the solution (always the same one).


Alex
 
R

Radioactive Man

Something like:

import urllib
urllib.urlretrieve( "htttp://www.sound.com/files/bob.wav",
'c:/sound/bob.wav')

should be fine.


http://docs.python.org/lib/module-urllib.html has the docs of
urlretrieve, and it's SO simple that I don't really see what "good
examples" one might provide. A book that by design is full of examples
(hopefully good) is the Python Cookbook, which in the intro to Chapter
10 (Network Programming) gives a slightly richer example which downloads
several files with urllib.urlretrieve.
http://www.python9.org/p9-zadka.ppt may have some useful pointers,
perhaps.

http://www.experts-exchange.com/Programming/Programming_Languages/Python
/Q_21048439.html has exactly the same question you asked, but you have
to "sign up" to see the solution (always the same one).


Alex


Thanks to all who replied on this one. I have managed to download,
but somehow the file is getting mangled when I save it to my hard
drive. Here is the test script I came up with:

import urllib
f = urllib.urlopen("http://www.python.org/pics/pythonHi.gif")
g = f.read()
file = open("trash.gif", "w")
file.write(g)
file.close()

The file "trash.gif" is actually saved under "C:\Python23" in the
correct format (recognizable to *.gif viewing programs), but is so
severely mangled that I can't even recognize it visually. It happens
every time, so I do not believe it is a random event. My question
here is what am I doing wrong and at what stage is the file getting
mangled? I know this method works fine with text files, but for some
reason is damaging to binary files.

The same result happened when I substituted urllib.urlretrieve() for
urllib.urlopen().
 
A

Andrew Durdin

Here is the test script I came up with:

import urllib
f = urllib.urlopen("http://www.python.org/pics/pythonHi.gif")
g = f.read()
file = open("trash.gif", "w")
file.write(g)
file.close()

You're opening the output file in ascii mode, so newlines characters
are getting converted to the CR/LF combination. Change the second
parameter to open() to "wb".
 
R

Radioactive Man

You're opening the output file in ascii mode, so newlines characters
are getting converted to the CR/LF combination. Change the second
parameter to open() to "wb".

That worked. Thanks.

Looking back through the documentation, I see that open() is really an
alias for file(). Are there any variants of the "open" commmand that
allow location (drive and directory) of the file to be specified as
well, for example, if I wanted to save the file as
"D:\binaries\trash.gif" instead of the default location? If that's
not possible, then the alternative might be exectuting commands from
the os module to accomplish the same thing.
 
J

Jeff Shannon

Radioactive said:
Looking back through the documentation, I see that open() is really an
alias for file().

For now... but (despite what those docs imply) it's really best to stick
with using open() and don't worry about the existence of file() unless
you're trying to subclass it... (GvR has recently said that he never
intended for file() to replace open(), and that the direct use of file()
is not preferred... which surprised a lot of us, apparently including
the person who wrote those docs. :) )
Are there any variants of the "open" commmand that
allow location (drive and directory) of the file to be specified as
well, for example, if I wanted to save the file as
"D:\binaries\trash.gif" instead of the default location?

As a matter of fact, the standard open() will handle that just fine --
it accepts a pathname, rather than just a filename, so you can feed it
an absolute path like your example or a relative path (e.g.,
subdir\trash.gif or ..\siblingdir\trash.gif) and it'll be perfectly
happy. The one catch is that, on Windows, the directory separator
conflicts with the escape character, '\'. So when typing path literals,
be sure to either always use double backslashes (
"d:\\binaries\\trash.gif" ) or 'raw' strings ( r"d:\binaries\trash.gif"
) so that your \trash doesnt become [tab]rash. Or better yet, use
os.path.join() (and the other os.path functions) to construct your paths.

Jeff Shannon
Technician/Programmer
Credit International
 
D

Dennis Lee Bieber

happy. The one catch is that, on Windows, the directory separator
conflicts with the escape character, '\'. So when typing path literals,
be sure to either always use double backslashes (
"d:\\binaries\\trash.gif" ) or 'raw' strings ( r"d:\binaries\trash.gif"
) so that your \trash doesnt become [tab]rash. Or better yet, use
os.path.join() (and the other os.path functions) to construct your paths.
When used in direct programming calls (like open() ), Windows
seems happy with "/"s. It is only in things like os.system() where the
string is passed to an external command interpreter that you MUST use
the "\"s.

--
 
P

phansen

Jeff said:
As a matter of fact, the standard open() will handle that just fine --
it accepts a pathname, rather than just a filename, so you can feed it
an absolute path like your example or a relative path ...

But note that the directories have to exist already. If they don't,
you need to use something like os.makedirs() to get 'em.

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top