pythondropboxdropbox-apidropbox-sdk

I can't find my uploaded files in Dropbox


I'm working on uploading some countries' admin data to my Dropbox app. Here below is my code that does that:

# importing the required libraries
import dropbox, sys, os
import requests

# Get your app key and secret from the Dropbox developer website
app_key = 'qie********'
app_secret = 'qom**********'

dbx = dropbox.Dropbox('YYPRp-*******************_JzclLe-***************-3Js')

# verify if the account is connected
dbx.users_get_current_account()

#find all the folders present
for entry in dbx.files_list_folder('').entries:
    print(entry.name)
    

# creating a path to where all the data to be uploaded is
root_dir = "H:/WORK/Upwork/Project 7 - Python School Data Analysis/Planning and Costing Model/Updated Script/Extracted" 

print ("Attempting to upload...")


z = 1
for dir, dirs, files in os.walk(root_dir):
    # the first dir is the root dir itself so we skip it
    if z == 1:
        z = z + 1
        continue
    # uploading contents of the file path
    elif z > 15:
        # split the path to get the country, which is the very last item after split (-1)
        split_dir = dir.split('\\')
        folder_name = split_dir[-1] # country name
        # creating a new folder in my Dropbox for each country
        country_name = dbx.files_create_folder('/Data/'+ folder_name)
        dropbox_folder = country_name.path_display #obtaining the name of the folder
        folder_split = dropbox_folder.split('/') # splitting the path to get root folder and created folder
        folder_created = folder_split[-1] #created/country folder
        dest_path = os.path.join('/Data/', folder_created) #joining the two to make a full path
        print(dest_path)
        # looping through the files in each of the country's folder
        for file in files:
            try:
                # getting the path for each of the file in the folder
                file_path = os.path.join(dir, file)
                print(f'Uploading to {folder_name} in Dropbox')
                f = open(file_path, 'rb')
                connect = '/' # will be used to separate the destination path and the file
                # this is where the file will be saved
                d_path = os.path.join(dest_path, connect, file)
                dbx.files_upload(f.read(), d_path, mode=dropbox.files.WriteMode.overwrite)
                print(dest_path)
                print(file_path)
                print(dir)
                print('\n')
                
            except Exception as err:
                print("Error!", file, err)
    z = z + 1

The code runs successfully with no errors. Here is how it looks at the console:

enter image description here

It successfully creates the folders for each of the countries. Note that in my countries' folders, it has several files (max of 15). When I visit my dropbox app, the folders are there but nothing is inside the folders. There are completely not files, I receive the message notification that says:

This Folder is Empty

See below the images:

With folders created enter image description here

One of the countries, with no files: enter image description here

I have given it over an hour but nothing changes. Also note that I configured all permissions necessary for writing files and folders. Could there be something I may have done wrong? I will appreciate any assistance. Thanks!


Solution

  • After some help from Greg, I was able to find where the issue was. The files_upload class functions expects a working path as part of its parameters. The path provided could not be found in the App and so I added the following to make it work: d_path = dest_path+d_path

    Here is the full working code:

    # importing the required libraries
    import dropbox, sys, os
    import requests
    
    # Get your app key and secret from the Dropbox developer website
    app_key = 'qie********'
    app_secret = 'qom**********'
    
    dbx = dropbox.Dropbox('YYPRp-*******************_JzclLe-***************-3Js')
    
    # verify if the account is connected
    dbx.users_get_current_account()
    
    #find all the folders present
    for entry in dbx.files_list_folder('').entries:
        print(entry.name)
        
    
    # creating a path to where all the data to be uploaded is
    root_dir = "H:/WORK/Upwork/Project 7 - Python School Data Analysis/Planning and Costing Model/Updated Script/Extracted" 
    
    print ("Attempting to upload...")
    
    
    z = 1
    for dir, dirs, files in os.walk(root_dir):
        # the first dir is the root dir itself so we skip it
        if z == 1:
            z = z + 1
            continue
        # uploading contents of the file path
        elif z > 15:
            # split the path to get the country, which is the very last item after split (-1)
            split_dir = dir.split('\\')
            folder_name = split_dir[-1] # country name
            # creating a new folder in my Dropbox for each country
            country_name = dbx.files_create_folder('/Data/'+ folder_name)
            dropbox_folder = country_name.path_display #obtaining the name of the folder
            folder_split = dropbox_folder.split('/') # splitting the path to get root folder and created folder
            folder_created = folder_split[-1] #created/country folder
            dest_path = os.path.join('/Data/', folder_created) #joining the two to make a full path
            print(dest_path)
            # looping through the files in each of the country's folder
            for file in files:
                try:
                    # getting the path for each of the file in the folder
                    file_path = os.path.join(dir, file)
                    print(f'Uploading to {folder_name} in Dropbox')
                    f = open(file_path, 'rb')
                    connect = '/' # will be used to separate the destination path and the file
                    # this is where the file will be saved
                    d_path = os.path.join(dest_path, connect, file)
                    d_path = dest_path+d_path
                    dbx.files_upload(f.read(), d_path, mode=dropbox.files.WriteMode.overwrite)
                    print(dest_path)
                    print(file_path)
                    print(dir)
                    print('\n')
                    
                except Exception as err:
                    print("Error!", file, err)
        z = z + 1