How to download directly to a file?

Discussion in 'Python' started by kj, Sep 4, 2009.

  1. kj

    kj Guest

    I want to send a POST request and have the returned content put
    directly into a file. Is there a way to do this easily in Python?
    I've been looking at the documentation for urllib2, but I can't
    see a direct way to do this, other than saving the returned contents
    to an in-memory variable and writing out the variable to a file.
    But this is precisely what I'd like to avoid.

    Thanks!

    kynn
     
    kj, Sep 4, 2009
    #1
    1. Advertising

  2. kj schrieb:
    > I want to send a POST request and have the returned content put
    > directly into a file. Is there a way to do this easily in Python?
    > I've been looking at the documentation for urllib2, but I can't
    > see a direct way to do this, other than saving the returned contents
    > to an in-memory variable and writing out the variable to a file.
    > But this is precisely what I'd like to avoid.


    You get a file-like object, what's wrong reading that chunkwise &
    dumping that to a file? Or are 4KB blocksize to hard of a memory constraint?

    Diez
     
    Diez B. Roggisch, Sep 4, 2009
    #2
    1. Advertising

  3. kj

    kj Guest

    In <-berlin.de> "Diez B. Roggisch" <> writes:

    >kj schrieb:
    >> I want to send a POST request and have the returned content put
    >> directly into a file. Is there a way to do this easily in Python?
    >> I've been looking at the documentation for urllib2, but I can't
    >> see a direct way to do this, other than saving the returned contents
    >> to an in-memory variable and writing out the variable to a file.
    >> But this is precisely what I'd like to avoid.


    >You get a file-like object, what's wrong reading that chunkwise &
    >dumping that to a file? Or are 4KB blocksize to hard of a memory constraint?


    Actually, I discovered that urllib.urlretrieve does what I want.
    (I'd assumed that urllib2 superseded urllib, but apparently not.)

    kynn
     
    kj, Sep 5, 2009
    #3
  4. kj

    Chris Rebert Guest

    On Fri, Sep 4, 2009 at 4:35 PM, kj<> wrote:
    > In <-berlin.de> "Diez B. Roggisch" <> writes:
    >
    >>kj schrieb:
    >>> I want to send a POST request and have the returned content put
    >>> directly into a file.  Is there a way to do this easily in Python?
    >>> I've been looking at the documentation for urllib2, but I can't
    >>> see a direct way to do this, other than saving the returned contents
    >>> to an in-memory variable and writing out the variable to a file.
    >>> But this is precisely what I'd like to avoid.

    >
    >>You get a file-like object, what's wrong reading that chunkwise &
    >>dumping that to a file? Or are 4KB blocksize to hard of a memory constraint?

    >
    > Actually, I discovered that urllib.urlretrieve does what I want.
    > (I'd assumed that urllib2 superseded urllib, but apparently not.)


    urllib2 is an "advanced" version of the library that gives you more
    control over how things are done, but is also more complicated.
    Regular urllib is simpler and easier to use, but you don't have as
    much control either.
    Neither supersedes the other, to my knowledge.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Sep 5, 2009
    #4
    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:
    953
    Ryan Taylor
    Nov 10, 2004
  2. Steve C. Orr [MVP, MCSD]
    Replies:
    0
    Views:
    1,625
    Steve C. Orr [MVP, MCSD]
    Mar 7, 2005
  3. Sam --
    Replies:
    2
    Views:
    617
    Sam --
    Mar 17, 2005
  4. Brett  Kelly
    Replies:
    1
    Views:
    684
    Steve C. Orr [MVP, MCSD]
    Jun 16, 2006
  5. HosedIfSomeoneBadFiguresOutWhoIAm

    Browsers can download assemblies directly from my website's /bin d

    HosedIfSomeoneBadFiguresOutWhoIAm, Jul 1, 2004, in forum: ASP .Net Security
    Replies:
    3
    Views:
    153
    [MSFT]
    Jul 6, 2004
Loading...

Share This Page