You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.
Forum Discussion
muratowski
2 years agoHelpful | Level 5
Unable to list files in shared Dropbox folder using Python and the Dropbox API
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.
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.
Ok I changed my codebase a little bit and now it works perfectly!
Thank your for your efforts.
- Greg-DBDropbox Staff
[Cross-linking for reference: https://stackoverflow.com/questions/75725830/unable-to-list-files-in-shared-dropbox-folder-using-python-and-the-dropbox-api ]
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.
- muratowskiHelpful | Level 5
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-DBDropbox Staff
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.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
5,910 PostsLatest Activity: 4 days agoIf you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!