I wrote this code too sort documents in my dropbox but it stops each time at a certain file (the 24th one)

44 Views Asked by At
import os
import dropbox

# Replace 'YOUR_ACCESS_TOKEN' with your Dropbox access token
ACCESS_TOKEN = ''

def main():
    # Initialize the Dropbox client
    dbx = dropbox.Dropbox(ACCESS_TOKEN)

    # List all files in the source folder
    source_folder = "/ALL_NDA_SAMPLES"
    result = dbx.files_list_folder(source_folder, recursive=True)

    # List all files in the destination folder
    destination_folder = "/ALL_NDA_SAMPLES/!ALLVINTAGE"
    existing_files = set(f.name for f in dbx.files_list_folder(destination_folder).entries)

    # Copy files containing "Vintage" in the name to the destination folder
    for entry in result.entries:
        if isinstance(entry, dropbox.files.FileMetadata) and "Vintage" in entry.name:
            if entry.name not in existing_files:
                source_path = entry.path_display
                destination_path = os.path.join(destination_folder, entry.name)

                # Copy the file to the destination folder
                dbx.files_copy(source_path, destination_path)
                print(f"Copied: {entry.name}")
            else:
                print(f"Already copied: {entry.name}")

if __name__ == "__main__":
    main()

I tried to add Documents in between the scanned ones it doesnt recognize them either.

If you have any Idea or need more Details tell me :)

Greetings

1

There are 1 best solutions below

0
Greg On

There isn't enough information here to say if this is definitely the cause of the issue, but as written your code is incomplete and is not guaranteed to list all of the items in a folder.

The code you shared never checks has_more or calls files_list_folder_continue. You need to always check has_more and call back to files_list_folder_continue when it is True using the returned cursor in order to make sure you get all of the entries.

The files_list_folder method isn't guaranteed to return all of the entries in a single response. There are a few factors that can affect how much is returned in a single call, and that can vary over time and by path, so you need to make sure you always have that implemented properly.

Refer to the linked documentation for more information on using this functionality.