Recently, I came across an interesting project involving Dropbox. The client manages a services business where they take hundreds of images per project and upload them to Dropbox. Then they have to re-upload the images to their internal system for which the images where required to be renamed following certain patterns.
The Problem: How to rename thousands of files in Dropbox folders?
Renaming thousands of files on Dropbox manually is a pain, especially when the files have to be renamed based on certain patterns.
I was asked to write an app that will:
- Connect with Dropbox
- Ask for a folder and make sure it exists
- Scan the folder for image files. In most cases the images will be stored in Zip archives which have to decompressed. Since Dropbox does not allow decompression of zip files, such files have to downloaded, unzipped and uploaded again.
- Rename files that match certain pattern
The Solution: A Python script to rename files using Dropbox API
I decided to write a Python script that will connect to Dropbox API using Python SDK and rename the files based on the requirements.
In order to work with Dropbox API, it is required to create a Dropbox App and generate the access token.
Create a Dropbox App and get Access Token:
- Visit the Dropbox App Console. https://www.dropbox.com/developers/apps/
- Click the Create app button.
- From Choose an API section select Dropbox API.
- From Choose the type of access you need section select Full Dropbox.
- Enter a name for your app, — << I have selected say ImageRenamer >>
- Click on Create app button.
- On the next screen, click on Generate to generate access token.
An access token will be generated.
Install Dropbox SDK and brew some Python magic
Install Dropbox Python SDK:
pip install dropbox
import dropbox import os import zipfile
Code to rename files in using Python SDK and Dropbox API:
# input name of image folder image_folder = str(input("Enter folder name to fetch images: ")).lower() # instantiate Dropbox connection dbx = dropbox.Dropbox('dropbox access token') # proceed if image folder exists in Dropbox if dbx.files_get_metadata(image_folder).path_lower == image_folder: # iterate through the files inside Dropbox folder for file in dbx.files_list_folder(image_folder).entries: # if zip files found, download, unzip and upload if file.name.endswith(".zip"): # download zip file and save locally with open("downloaded_zip.zip", "wb") as f: metadata, res = dbx.files_download(file.path_lower) f.write(res.content) # extract zip file using Python zipfile module # folder name "image_folder" is same as on Dropbox with zipfile.ZipFile("downloaded_zip.zip", "r") as zip_ref: zip_ref.extractall(image_folder) # upload extracted images to Dropbox for root, dirs, files in os.walk(images_folder, topdown=False): for name in files: file_to_upload = os.path.join(root, name) # create upload path and replace \ with / :: a work around from windows to linux file_upload_path = dropbox_folder + file_to_upload.replace(os.path.sep, '/')[3:] with open(file_to_upload, "rb") as f: dbx.files_upload(f.read(), file_upload_path, mute=True) # rename images files in Dropbox folder and sub-folders for entry in dbx.files_list_folder(image_folder, recursive=True, include_deleted=False).entries: # files_move_v2 will move file from source to destination path # if both paths are same, the file will be essentially renamed. dbx.files_move_v2(entry.path_display, new_file_path)
** Above code is proof of concept. It does not cover catching exceptions and applying business logic.
Finally the code was packaged in an .exe executable using PyInstaller. Running the application will give a set of options to perform actions accordingly.
Creating a Dropbox app [link]
Signup for a free Dropbox account [link]
Python Dropbox SDK GitHub [link]
Python Dropbox SDK documentation [link]
Freelance Python Developer in Karachi, Pakistan. Available for Web Scraping | Data Mining | Web Bots | Python Development. [Check my Portfolio here]
If you found the above post interesting, join my list to get updated when the next one comes up.
For consulting assignments, contact me here with your requirements.