使用Python在Dropbox上创建一个空文件夹。有没有更简单的方法?

时间:2020-06-21 14:14:17

标签: python-3.x dropbox dropbox-api

这是我的示例代码,可以正常工作:

import os, io, dropbox

def createFolder(dropboxBaseFolder, newFolder):
    # creating a temp dummy destination file path
    dummyFileTo = dropboxBaseFolder + newFolder + '/' + 'temp.bin'

    # creating a virtual in-memory binary file
    f = io.BytesIO(b"\x00")

    # uploading the dummy file in order to cause creation of the containing folder        
    dbx.files_upload(f.read(), dummyFileTo)
    
    # now that the folder is created, delete the dummy file    
    dbx.files_delete_v2(dummyFileTo)

accessToken = '....'
dbx = dropbox.Dropbox(accessToken)

dropboxBaseDir = '/test_dropbox'
dropboxNewSubDir = '/new_empty_sub_dir'

createFolder(dropboxBaseDir, dropboxNewSubDir)

但是有没有更有效/更简单的方法来完成任务?

1 个答案:

答案 0 :(得分:0)

是的,正如Ronald在评论中提到的那样,您可以使用the files_create_folder_v2 method创建一个新文件夹。

看起来像这样,修改您的代码:

import dropbox

accessToken = '....'
dbx = dropbox.Dropbox(accessToken)

dropboxBaseDir = '/test_dropbox'
dropboxNewSubDir = '/new_empty_sub_dir'

res = dbx.files_create_folder_v2(dropboxBaseDir + dropboxNewSubDir)
# access the information for the newly created folder in `res`