How to download directly to a file?

K

kj

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
 
D

Diez B. Roggisch

kj said:
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
 
K

kj

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
 
C

Chris Rebert

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
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top