Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Need script to download file at known address

Reply
Thread Tools

Need script to download file at known address

 
 
Radioactive Man
Guest
Posts: n/a
 
      09-15-2004
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.
 
Reply With Quote
 
 
 
 
Pierre Fortin
Guest
Posts: n/a
 
      09-15-2004
On Wed, 15 Sep 2004 00:00:21 GMT Radioactive wrote:

> 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...

>>> from urllib import urlopen
>>> URL1 = "http://ichart.finance.yahoo.com/table.csv?"
>>> URL2 = "s=IBM&a=08&b=14&c=2004&d=08&e=14&f=2004&g=d&ignor e=.csv"
>>> urlopen("%s%s" % (URL1,URL2)).readlines()

['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']
>>>

 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      09-15-2004
Radioactive Man <> wrote:

> 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/Prog...nguages/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
 
Reply With Quote
 
Radioactive Man
Guest
Posts: n/a
 
      09-17-2004
On Wed, 15 Sep 2004 17:20:43 +0200, (Alex Martelli)
wrote:

>Radioactive Man <> wrote:
>
>> 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/Prog...nguages/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().
 
Reply With Quote
 
Andrew Durdin
Guest
Posts: n/a
 
      09-17-2004
On Fri, 17 Sep 2004 01:46:42 GMT, Radioactive Man <> wrote:
> 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".
 
Reply With Quote
 
Radioactive Man
Guest
Posts: n/a
 
      09-17-2004
On Fri, 17 Sep 2004 12:08:36 +1000, Andrew Durdin <>
wrote:

>On Fri, 17 Sep 2004 01:46:42 GMT, Radioactive Man <> wrote:
>> 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".


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.

 
Reply With Quote
 
Jeff Shannon
Guest
Posts: n/a
 
      09-17-2004
Radioactive Man wrote:

>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

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      09-17-2004
On Thu, 16 Sep 2004 19:59:17 -0700, Jeff Shannon <>
declaimed the following in comp.lang.python:

> 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.

--
> ================================================== ============ <
> | Wulfraed Dennis Lee Bieber KD6MOG <
> | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

 
Reply With Quote
 
phansen
Guest
Posts: n/a
 
      09-17-2004
Jeff Shannon wrote:
> Radioactive Man wrote:
>> 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 ...


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need your experience: is "(void)param;" to avoid unused variablewarnings well known for you? Qi C++ 22 04-05-2012 06:17 AM
Direct Download Movies - No Download Limits - Download DivX DVDMovies hussain dandan Python 0 12-06-2009 04:52 AM
Download dialog for known MIME type mizi ASP General 0 10-30-2005 10:38 AM
Get hostname, when IP address is known vi Java 5 09-19-2005 10:36 PM
Need Help: C++ Delete Known File da Vinci C++ 23 06-28-2004 04:40 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57