Создание пустой папки в Dropbox с помощью Python. Есть способ попроще? - PullRequest
0 голосов
/ 21 июня 2020

Вот мой пример кода, который работает:

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 голосов
/ 22 июня 2020

Да, как Рональд упомянул в комментариях, вы можете использовать метод files_create_folder_v2 для создания новой папки.

Это будет выглядеть так, изменив свой код:

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`
...