Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   How to download directly to a file? (http://www.velocityreviews.com/forums/t697166-how-to-download-directly-to-a-file.html)

kj 09-04-2009 09:47 PM

How to download directly to a file?
 



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

Diez B. Roggisch 09-04-2009 09:58 PM

Re: How to download directly to a file?
 
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

kj 09-04-2009 11:35 PM

Re: How to download directly to a file?
 
In <7gdgslF2ogf8rU1@mid.uni-berlin.de> "Diez B. Roggisch" <deets@nospam.web.de> 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

Chris Rebert 09-05-2009 03:23 AM

Re: How to download directly to a file?
 
On Fri, Sep 4, 2009 at 4:35 PM, kj<no.email@please.post> wrote:
> In <7gdgslF2ogf8rU1@mid.uni-berlin.de> "Diez B. Roggisch" <deets@nospam.web.de> 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


All times are GMT. The time now is 01:56 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.