The code samples in this tutorial use the
.
Prerequisites
- A Box application with the scope (
root_readwrite) enabled - Python 3.8 or later installed on your computer
- The Box Python SDK installed (
pip install "boxsdk~=10") - An authenticated
- Completion of the tutorial, which defines the
file_uploadandfile_upload_chunkedhelper functions this tutorial reuses to avoid duplicating upload logic
Handle duplicate folders
Before you access a subfolder, check whether it already exists in Box. If a folder with the same name exists, use the existing folder instead of creating a new one:Python v10
Recursively upload a folder
Combine the helper functions into a single recursive function that walks the local directory tree. For each item, the function:- Creates the folder in Box, or retrieves the existing folder, and then recurses into it.
- Checks the file size to choose a direct or chunked upload. Files that are 20 MB or larger use chunked upload; smaller files use direct upload.
- Uploads a new version when a file with the same name already exists, by using the helpers from the upload methods tutorial.
Python v10
min_chunked_size parameter defaults to 20 MB, which is the minimum size for
. You can
increase this threshold, but you cannot decrease it.
Complete example
The following script authenticates a Box client, creates a destination folder (or reuses an existing one), and recursively uploads the contents of a local directory. It reuses thecreate_box_folder helper to get or create the
destination folder in the root folder ("0"):
Python v10
