You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派Dropbox自动上传脚本:指定目标文件夹需求问询

Solution: Upload to Specific Dropbox Folder from Raspberry Pi Automatically

Hey there! I’ve run into this exact problem before with Dropbox scripts on Raspberry Pi—let’s get your files uploading to that specific folder without a hassle. Here’s a step-by-step fix:

1. Update Your Upload Script to Target a Specific Folder

Assuming you’re using the Dropbox Python SDK (super common for Pi scripts), we’ll modify the script to explicitly define your target folder path. If you haven’t installed the SDK yet, run this first:

pip3 install dropbox

Here’s a revised script that lets you set a target folder, plus handles creating the folder if it doesn’t exist:

import dropbox
import os

# Replace with your Dropbox access token (keep this secret!)
DROPBOX_TOKEN = "your_dropbox_access_token"
LOCAL_FILE = "/home/pi/path/to/your/local/file.txt"  # Your local file to upload
TARGET_FOLDER = "/RaspberryPi/Backups/"  # Your desired Dropbox folder (start/end with /)

def upload_to_specific_folder(local_file_path, dropbox_target_folder):
    dbx = dropbox.Dropbox(DROPBOX_TOKEN)
    file_name = os.path.basename(local_file_path)
    full_dropbox_path = dropbox_target_folder + file_name

    # Create target folder if it doesn't exist
    try:
        dbx.files_get_metadata(dropbox_target_folder)
    except dropbox.exceptions.ApiError as e:
        if isinstance(e.error, dropbox.files.GetMetadataError) and e.error.is_path() and e.error.get_path().is_not_found():
            dbx.files_create_folder_v2(dropbox_target_folder)
            print(f"Created missing folder: {dropbox_target_folder}")

    # Upload the file
    with open(local_file_path, "rb") as file:
        dbx.files_upload(
            file.read(),
            full_dropbox_path,
            mode=dropbox.files.WriteMode.overwrite  # Overwrite existing file, use "add" to rename instead
        )
    print(f"Uploaded {file_name} to {full_dropbox_path} successfully!")

if __name__ == "__main__":
    upload_to_specific_folder(LOCAL_FILE, TARGET_FOLDER)

Key Notes:

  • TARGET_FOLDER: Make sure it starts and ends with a slash (e.g., /MyFolder/) to avoid path errors.
  • Write Mode: Use overwrite to replace existing files, or add if you want to keep multiple versions (Dropbox will append a number to duplicates).
  • Access Token: Get this from the Dropbox App Console (create a "Scoped App" with "files.content.write" and "files.content.read" permissions).

2. Set Up Automatic Execution with Cron

To make this run on a schedule (e.g., daily backups), use Raspberry Pi’s built-in cron scheduler:

  1. Open the cron editor:
    crontab -e
    
  2. Add a line at the bottom to define your schedule. For example, to run every day at 2 AM:
    0 2 * * * /usr/bin/python3 /home/pi/path/to/your/upload_script.py >> /home/pi/dropbox_upload_logs.log 2>&1
    
    • /usr/bin/python3: Explicitly uses Pi’s Python 3 (avoids environment variable issues)
    • Log file: Writes output/errors to dropbox_upload_logs.log so you can troubleshoot if something fails.

Troubleshooting Tips

  • If you get permission errors, double-check your Dropbox app’s scopes (make sure it can access the target folder).
  • Test the script manually first with python3 /home/pi/path/to/your/upload_script.py to confirm it works before setting up cron.

Give this a try—let me know if you hit any roadblocks, and I’ll help you sort it out!

内容的提问来源于stack exchange,提问作者Challenger88

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:58:52