简体   繁体   中英

Downloading all of the files in a specific folder with PyDrive

My Google Drive looks something like this:

picture1.jpg

song1.mp3

a/b/file1.txt

a/b/file2.txt

a/b/file3.jpg

a/b/file4.m4a

a/b/...

I want to use PyDrive to download every file in folder "b". After looking at the documentation and checking StackOverflow, I still cannot figure out how to do this. How would I download all of the files in folder "b" (which is contained in folder "a") using PyDrive. Also, in case it's relevant please note that folder "b" contains thousands of files.

I figured it out. Basically you need to use the file id to list or download a folder's contents.

Assuming that file_list is the root directory:

for file1 in file_list:
    if file1['title'] == '[name_of_target_folder]':
        folder_id = file1['id']

Then

> folder_id 
> 'WIU1xyz19g83abcdefg'

(for example)

get every file in 'folder':

file_list = drive.ListFile({'q': "'{}' in parents and trashed=false".format(folder_id)}).GetList()

download every file in 'folder':

for i, file1 in enumerate(sorted(file_list, key = lambda x: x['title']), start=1):
    print('Downloading {} from GDrive ({}/{})'.format(file1['title'], i, len(file_list)))
    file1.GetContentFile(file1['title'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM