【问题标题】:I can't find my uploaded files in Dropbox我在 Dropbox 中找不到我上传的文件
【发布时间】:2022-01-10 17:46:36
【问题描述】:

我正在努力将一些国家/地区的管理数据上传到我的 Dropbox 应用。下面是我的代码:

# 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

代码运行成功,没有错误。这是控制台的外观:

它成功地为每个国家/地区创建了文件夹。请注意,在我所在国家/地区的文件夹中,它有多个文件(最多 15 个)。当我访问我的保管箱应用程序时,文件夹在那里,但文件夹内没有任何内容。完全没有文件,我收到消息通知说:

此文件夹为空

见下图:

创建文件夹

没有文件的国家之一:

我已经给了它一个多小时,但没有任何改变。另请注意,我配置了写入文件和文件夹所需的所有权限。我可能做错了什么吗?我将不胜感激任何帮助。谢谢!

【问题讨论】:

  • 需要检查的几件事:a) 打印出d_path 以确保您指定了您期望的路径,b) 打印出files_upload 调用的结果(它应该包含上传文件的元数据),c) 确保您正在使用具有您期望的访问类型的应用程序的访问令牌,并且 d) 确保您正在使用连接到您期望的帐户的访问令牌。
  • 感谢@Greg 指出这一点。问题实际上出在d_path 中,这不是一条完整的路径。我已经纠正它,一切似乎都很好!

标签: python dropbox dropbox-api dropbox-sdk


【解决方案1】:

在 Greg 的帮助下,我找到了问题所在。 files_upload 类函数需要一个工作路径作为其参数的一部分。提供的路径在应用程序中找不到,所以我添加了以下内容以使其工作:d_path = dest_path+d_path

这是完整的工作代码:

# 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

【讨论】:

    猜你喜欢
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多