Skip to content

Instantly share code, notes, and snippets.

Created February 22, 2017 09:02
Show Gist options
  • Save Keshava11/d14db1e22765e8de2670b8976f3c7efb to your computer and use it in GitHub Desktop.
Save Keshava11/d14db1e22765e8de2670b8976f3c7efb to your computer and use it in GitHub Desktop.
Simple python script to upload file to Dropbox.
# Prerequisites :
# 1.SetUp dropbox sdk to be able to use Dropbox Api's
# $ sudo pip install dropbox
# By default python dropbox sdk is based upon the python 3.5
# 2. Create an App on dropbox console ( which will be used and validated to do
# the file upload and restore using dropbox api. Mostly you need an access token to connect to Dropbox before actual file/folder operations.
# 3. Once done with code, run the script by following command
# $ python // if python3.5 is default
import sys
import dropbox
from dropbox.files import WriteMode
from dropbox.exceptions import ApiError, AuthError
# Access token
TOKEN = '<Generated after after App creation in step 2 in Prerequisites>'
LOCALFILE = '<Path to the file to be uploaded>'
BACKUPPATH = '/<desitination filename>' # Keep the forward slash before destination filename
# Uploads contents of LOCALFILE to Dropbox
def backup():
with open(LOCALFILE, 'rb') as f:
# We use WriteMode=overwrite to make sure that the settings in the file
# are changed on upload
print("Uploading " + LOCALFILE + " to Dropbox as " + BACKUPPATH + "...")
dbx.files_upload(, BACKUPPATH, mode=WriteMode('overwrite'))
except ApiError as err:
# This checks for the specific error where a user doesn't have enough Dropbox space quota to upload this file
if (err.error.is_path() and
sys.exit("ERROR: Cannot back up; insufficient space.")
elif err.user_message_text:
# Adding few functions to check file details
def checkFileDetails():
print("Checking file details")
for entry in dbx.files_list_folder('').entries:
print("File list is : ")
# Run this script independently
if __name__ == '__main__':
# Check for an access token
if (len(TOKEN) == 0):
sys.exit("ERROR: Looks like you didn't add your access token. Open up in a text editor and paste in your token in line 14.")
# Create an instance of a Dropbox class, which can make requests to the API.
print("Creating a Dropbox object...")
dbx = dropbox.Dropbox(TOKEN)
# Check that the access token is valid
except AuthError as err:
"ERROR: Invalid access token; try re-generating an access token from the app console on the web.")
except Error as err:
sys.exit("Error while checking file details")
print("Creating backup...")
# Create a backup of the current settings file
Copy link

tested and running with python 3.6.2, thanks!

Copy link

Also works perfectly with Python 3.5.2

Copy link

Everything seems to run, except it is having trouble finding the local file:
Traceback (most recent call last):
File "C:/Users/doug/PycharmProjects/Dropbox/d14db1e22765e8de2670b8976f3c7efb-561175ef159fb63c8330805b56336be5a5fa80bc/", line 82, in
File "C:/Users/doug/PycharmProjects/Dropbox/d14db1e22765e8de2670b8976f3c7efb-561175ef159fb63c8330805b56336be5a5fa80bc/", line 30, in backup
with open(LOCALFILE, 'rb') as f:

OSError: [Errno 22] Invalid argument: 'D:\PythonData\x0cile1.png'

Windows 10, python 3.6

Copy link

Never mine.... I figured out that the "" should be "/"

Copy link

Exception has occurred: AttributeError
'UploadWriteFailed' object has no attribute 'error'
File "/Users/Yen/AI應用/dropbox-sdk-python-master/", line 45, in backup
File "/Users/Yen/AI應用/dropbox-sdk-python-master/", line 81, in

Copy link

rob93c commented May 25, 2019

This is so easy to understand, thank you for writing this!

Copy link

chchwy commented Nov 8, 2023

This is great. I confirmed this short script works with Python 3.10 in 2023.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment