Skip to content

Instantly share code, notes, and snippets.

@feelinc
Created June 30, 2015 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feelinc/a0b241762a31d9fa14fe to your computer and use it in GitHub Desktop.
Save feelinc/a0b241762a31d9fa14fe to your computer and use it in GitHub Desktop.
Upload folder content to Dropbox
#!/usr/bin/python
import os
import sys
from dropbox.client import DropboxClient
# get an access token, local (from) directory, and Dropbox (to) directory
# from the command-line
access_token, local_directory, dropbox_destination = sys.argv[1:4]
client = DropboxClient(access_token)
# enumerate local files recursively
for root, dirs, files in os.walk(local_directory):
for filename in files:
# construct the full local path
local_path = os.path.join(root, filename)
# construct the full Dropbox path
relative_path = os.path.relpath(local_path, local_directory)
dropbox_path = os.path.join(dropbox_destination, relative_path)
# check if local path already exist
print 'Searching "%s" for "%s"' % (dropbox_path.replace('/' + filename, ''), filename)
found = client.search(dropbox_path.replace('/' + filename, ''), filename)
# upload the file
if found:
print "Path found on Dropbox, this shouldn't happen! Skipping %s..." % filename
else:
with open(local_path, 'rb') as f:
client.put_file(dropbox_path, f)
UploadDirDropbox.py thedropboxaccesstoken /path/to/local/folder /path/to/dropbox/folder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment