cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Unable to list files in shared Dropbox folder using Python and the Dropbox API

Unable to list files in shared Dropbox folder using Python and the Dropbox API

muratowski
Helpful | Level 5
Go to solution
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.
1 Accepted Solution

Accepted Solutions

muratowski
Helpful | Level 5
Go to solution

Ok I changed my codebase a little bit and now it works perfectly!

 

Thank your for your efforts.

View solution in original post

5 Replies 5

Greg-DB
Dropbox Staff
Go to solution

[Cross-linking for reference: https://stackoverflow.com/questions/75725830/unable-to-list-files-in-shared-dropbox-folder-using-pyt... ]

 

Which line is resulting in that error? It looks like that error is probably referring to a local filesystem operation, not a Dropbox API call.

 

Also, while it's probably not related to this particular error, there are some other things to note for the Dropbox API methods:

  • You've implemented files_list_folder but not files_list_folder_continue. You're not guaranteed to get all of the entries returned in one call to files_list_folder so make sure you've implemented files_list_folder_continue as well. Check out the linked documentation for more information.
  • You're relying on 'entries[-1]' to find a folder entry, but note that the position of specific entries in any particular page of results in not guaranteed.

muratowski
Helpful | Level 5
Go to solution

Hi,

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!"

Greg-DB
Dropbox Staff
Go to solution

The os.listdir method is a Python method for listing the contents of a particular folder on the local filesystem at the specific path. It accesses the files and folders on the device and a FileNotFoundError error from it would indicate that there is nothing at the local path you specified, in this case using the value in the folder_path variable. The method does not call the Dropbox API and is not made by Dropbox though, so Dropbox can't offer support for that in particular.

 

I see you are getting the folder_path value from the files_list_folder results. That would only work with os.listdir if the path to the relevant folder on your local machine happened to match the path to it inside your Dropbox account.

 

I don't have full context on your project or the requirements and constraints you're working under of course, so I can't tell you exactly what code you should write. It may be helpful to step through with a debugger to see what each line is doing and update your code accordingly.

 

It may also be worth mentioning that if you ultimately want to move or rename files or folders in Dropbox, you can do so directly using the Dropbox API (whether or not you have your Dropbox folder synced to your computer locally), without interacting with the local filesystem. To move or rename a file or folder using the Dropbox API via the Dropbox Python SDK, you would use files_move_v2. And as noted earlier, you can use files_list_folder and files_list_folder_continue to list the contents of a folder (even without a shared link, if the content is in your connected Dropbox account). You may also want to check out the Getting Started and File Access guides.

muratowski
Helpful | Level 5
Go to solution

Thank you for the explanation.

 

I would like a Python script that can rename all the subdirectories within a specific main directory in my Dropbox account. The subdirectory names are currently in the format 'dd.mm.yyyy', but I would like them to be changed to the format 'yyyy-mm-dd'. I would like to be able to run this script by providing a link to the specific folder in my Dropbox account. If you have any simpler code snippet for it, I'd be happy to see that.

muratowski
Helpful | Level 5
Go to solution

Ok I changed my codebase a little bit and now it works perfectly!

 

Thank your for your efforts.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    muratowski Helpful | Level 5
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?