0

I am trying to use Python and the Dropbox API to list the files in a shared Dropbox folder.

Here is my source code:

import dropbox
import os

# Replace "ACCESS_TOKEN" with your Dropbox access token
ACCESS_TOKEN = "MY_ACCESS_TOKEN"

# Replace "https://www.dropbox.com/sh/..." with the shared link to the folder containing the directories you want to rename
shared_link_url = "d"

# Initialize the Dropbox API client
dbx = dropbox.Dropbox(ACCESS_TOKEN)

# Get the SharedLinkMetadata for the shared link
shared_link_metadata = dbx.sharing_get_shared_link_metadata(url=shared_link_url)

# Create a SharedLink object from the SharedLinkMetadata
shared_link = dropbox.files.SharedLink(url=shared_link_metadata.url)

# Get the list of files in the shared link
shared_link_metadata = dbx.files_list_folder(path="", shared_link=shared_link)
entries = shared_link_metadata.entries

# Get the path of the folder containing the directories to be renamed
folder_path = entries[-1].path_display

# Get a list of the directories in the folder
directories = os.listdir(folder_path)

# Loop through each directory and rename it
for directory in directories:
    try:
        if os.path.isdir(os.path.join(folder_path, directory)):
            # Check if the directory name can be split into three parts using a dot as a separator
            if len(directory.split(".")) != 3:
                print(f"Skipping {directory} - not in day.month.year format")
                continue

            # Split the directory name into day, month, and year components
            day, month, year = directory.split(".")

            # Combine the components into the new directory name format
            new_directory_name = f"{year}-{month}-{day}"

            # Get the current path and the new path for the directory
            current_path = os.path.join(folder_path, directory)
            new_path = os.path.join(folder_path, new_directory_name)

            # Rename the directory locally
            os.rename(current_path, new_path)

            # Upload the renamed directory to Dropbox
            with open(new_path, "rb") as f:
                dbx.files_upload(f.read(), f"/{new_directory_name}", mode=dropbox.files.WriteMode("overwrite"))

    except FileNotFoundError:
        print(f"Directory {directory} not found.")
        continue

I'm trying to rename directories with dates in specific paths in my Dropbox account using Python and the Dropbox API. However, I keep getting the same error message, 'FileNotFoundError: [Errno 2] No such file or directory,' even though I have granted all necessary permissions in both the Dropbox console and file sharing settings. How can I resolve this issue and successfully rename the directories?. Thanks!

I'm trying to rename directories with dates in specific paths in my Dropbox account using Python and the Dropbox API.

3
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example.
    – Community Bot
    Commented Mar 13, 2023 at 19:50
  • [Cross-linking for reference: dropboxforum.com/t5/Dropbox-API-Support-Feedback/… ]
    – Greg
    Commented Mar 13, 2023 at 20:21
  • Hi, FYI I got an error on line 27 of my code: ---> 27 directories = os.listdir(folder_path) Do you have any alternative code suggestions? Also, if possible, could you show me where to insert the code changes in my base? FYI I run the script in Google Colab. Thanks!"
    – muratowski
    Commented Mar 13, 2023 at 20:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.