Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function like urllib.urlretrieve for downloading files #885

Closed
matrixik opened this issue Oct 8, 2012 · 7 comments
Closed

Function like urllib.urlretrieve for downloading files #885

matrixik opened this issue Oct 8, 2012 · 7 comments

Comments

@matrixik
Copy link

matrixik commented Oct 8, 2012

Hello,

I would like to request function for downloading files.
Something like:
r = requests.get_file('https://example.com/big_file.zip', 'path_to_file')

I'm looking for it mainly for downloading big files (over 1GB), without fully loading them to memory.

Best regards

@kennethreitz
Copy link
Contributor

You can write a file without loading it into memory currently:

r = requests.get('https://example.com/big_file.zip'', prefetch=False)

with open('file', 'w') as f:
    for chunk in r.iter_chunks():
            f.write(chunk)

@matrixik
Copy link
Author

matrixik commented Oct 8, 2012

Nice to know but still I would prefer short one-liner over 4 lines.
P.S. You probably should update you example from requests.get_file to requests.get

@kennethreitz
Copy link
Contributor

This isn't about lines of code — it's about concise, succinct code. There is little value and many disadvantages of having a library write to files directly on your behalf.

@matrixik
Copy link
Author

matrixik commented Oct 8, 2012

Thank you for reply. I'll leave this to more experienced than me and use your code.

iter_chunks not exist, I need to use iter_lines or iter_content with big chunk_size

Best regards

@kennethreitz
Copy link
Contributor

Sorry, I hadn't had any coffee yet :)

@amitt001
Copy link

amitt001 commented May 4, 2016

For latest version(Edit: v2+) of requests:

r = requests.get('https://example.com/big_file.zip'', stream=True)

with open('file', 'wb') as f:
    for chunk in r.iter_content():
            f.write(chunk)

@Lukasa
Copy link
Member

Lukasa commented May 4, 2016

@amitt001 That should work in the current version v2.10.0, and indeed in all of the v2.X.X series release.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants