Need script to download file at known address

Discussion in 'Python' started by Radioactive Man, Sep 15, 2004.

  1. 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.
     
    Radioactive Man, Sep 15, 2004
    #1
    1. Advertising

  2. 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&ignore=.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']
    >>>
     
    Pierre Fortin, Sep 15, 2004
    #2
    1. Advertising

  3. 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/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
     
    Alex Martelli, Sep 15, 2004
    #3
  4. 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/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().
     
    Radioactive Man, Sep 17, 2004
    #4
  5. 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".
     
    Andrew Durdin, Sep 17, 2004
    #5
  6. 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.
     
    Radioactive Man, Sep 17, 2004
    #6
  7. Radioactive Man

    Jeff Shannon Guest

    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
     
    Jeff Shannon, Sep 17, 2004
    #7
  8. 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/> <
     
    Dennis Lee Bieber, Sep 17, 2004
    #8
  9. Radioactive Man

    phansen Guest

    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
     
    phansen, Sep 17, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan Taylor
    Replies:
    2
    Views:
    1,023
    Ryan Taylor
    Nov 10, 2004
  2. Sam --
    Replies:
    2
    Views:
    664
    Sam --
    Mar 17, 2005
  3. vi
    Replies:
    5
    Views:
    16,905
    Roedy Green
    Sep 19, 2005
  4. da Vinci
    Replies:
    23
    Views:
    11,882
    Robbie Hatley
    Jun 28, 2004
  5. mizi

    Download dialog for known MIME type

    mizi, Oct 30, 2005, in forum: ASP General
    Replies:
    0
    Views:
    178
Loading...

Share This Page