Eric J Ma's Website

How to programmatically download a file from Dropbox using Requests

written by Eric J. Ma on 2023-04-11 | tags: python dropbox file download programming requests library data storage jupyter notebooks portability internet connection content delivery personal cdn data files tutorials linux wget


Today I learned how to programmatically download a file from a Dropbox public folder. Storing it here so I remember how to do it in the future.

# Download the blog index from Dropbox using Requests.
# Taken from: https://stackoverflow.com/a/46005478
import requests
headers = {'user-agent': 'Wget/1.16 (linux-gnu)'}  # <-- the key is here!
r = requests.get("https://www.dropbox.com/s/xad2xf0n7isrhrz/blog_index.json?dl=0", stream=True, headers=headers)
with open("/tmp/blog_index.json", 'wb') as f:
    for chunk in r.iter_content(chunk_size=1024):
        if chunk:
            f.write(chunk)

I find it's useful to use Dropbox to host large data files for my Python-based tutorials, allowing me to ensure that my Jupyter notebooks are portable from computer to computer, as long as they can maintain an internet connection.

This is neat because it allows me to use Dropbox, for which I pay for storage, effectively as a personal CDN (content delivery network) with easily referenced URLs.


I send out a newsletter with tips and tools for data scientists. Come check it out at Substack.

If you would like to sponsor the coffee that goes into making my posts, please consider GitHub Sponsors!

Finally, I do free 30-minute GenAI strategy calls for organizations who are seeking guidance on how to best leverage this technology. Consider booking a call on Calendly if you're interested!