F128115
F128115

Reputation: 81

Can I download files from inside folder (Sub files) dropbox python?

Hi I am getting all folders like this

 entries=dbx.files_list_folder('').entries
   print (entries[1].name)
   print (entries[2].name)

And unable to locate subfiles in these folders. As I searched on internet but till now no working function I found.

Upvotes: 1

Views: 1570

Answers (2)

Sabuhi Shukurov
Sabuhi Shukurov

Reputation: 1926

There is two possible way to do that:

Either you can write the content to the file or you can create a link (either redirected to the browser or just get a downloadable link )

First way:

metadata, response = dbx.files_download(file_path+filename)
with open(metadata.name, "wb") as f:
    f.write(response.content)

Second way:

link = dbx.sharing_create_shared_link(file_path+filename)
print(link.url)

if you want link to be downloadable then replace 0 with 1:

path = link.url.replace("0", "1")

Upvotes: 0

Greg
Greg

Reputation: 16940

After listing entries using files_list_folder (and files_list_folder_continue), you can check the type, and then download them if desired using files_download, like this:

entries = dbx.files_list_folder('').entries

for entry in entries:
    if isinstance(entry, dropbox.files.FileMetadata):  # this entry is a file
        md, res = dbx.files_download(entry.path_lower)
        print(md)  # this is the metadata for the downloaded file
        print(len(res.content))  # `res.content` contains the file data

Note that this code sample doesn't properly paginate using files_list_folder_continue nor does it contain any error handling.

Upvotes: 2

Related Questions