DEV Community

Cover image for Synchronize a remote Dropbox directory with a local one in Python
Samuel Viana
Samuel Viana

Posted on • Originally published at codehouse.digfish.org

Synchronize a remote Dropbox directory with a local one in Python

After the last post, in which a simple local synchronizer were implemented in Python making use of the Watchdog library to detect the local changes and propagate them to a remote directory in Dropbox, I've decided to implement a reciprocal sychronizer that does the exact opposite - synchronizes continuously a remote Dropbox location to a local one. It's using the same Dropbox Python SDK as the latter post, and of course you'll need to register an app of yours in order to create a access token to acess the Dropbox API. In this particular case, a cursor is created every 30 seconds to poll for changes on the remote location and if that is the case, the API returns the name of involved file, and replicates locally what has happenned remotely. Again, in this case, the program does not perform a global synchronization between remote and local directories, only listens to modifications in real time and propagates them one-way from remote to local.

The code is below:

# directory sychornizer using python watchdog library with dropbox api
import os
import sys
import time
import logging
import dropbox
import threading
import configparser
from dropbox.files import DeletedMetadata, FileMetadata, FolderMetadata
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
root_dropbox_path = '/tmp'
local_path = 'D:/temp/dropbox-tmp'
def main():
config = configparser.ConfigParser()
dbx = dropbox.Dropbox("*****YOUR_ACCESS_TOKEN_HERE*****")
logging.info(dbx.users_get_current_account())
os.chdir(local_path)
while True:
cursor = dbx.files_list_folder_get_latest_cursor(root_dropbox_path,True)
longpoll_result = dbx.files_list_folder_longpoll(cursor.cursor,30)
if longpoll_result.changes:
filelist = dbx.files_list_folder_continue(cursor.cursor)
logging.info(filelist)
logging.info(filelist.entries)
for entry in filelist.entries:
if type(entry) == dropbox.files.DeletedMetadata : # deleted file
file_to_delete = entry.name
logging.info(f"Deleting {file_to_delete}")
if os.path.exists(file_to_delete):
os.unlink(file_to_delete)
elif type(entry) == dropbox.files.FileMetadata : # new file
file_to_download = entry.name
logging.info(f"Downloading {file_to_download} to {os.getcwd()}")
dbx.files_download_to_file(file_to_download,root_dropbox_path+'/'+file_to_download)
if __name__ == "__main__":
main()

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free