Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 545
James Shapiro Avatar asked Oct 29 '17 16:10

James Shapiro


People also ask

How do I download a specific directory in Python?

Download a file to a custom folder: To download a file to a specific folder, pass it the --directory-prefix or -P flag, followed by the destination folder. Interestingly, when the path to the folder doesn't exist, Wget will create it.


1 Answers

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'])
like image 54
James Shapiro Avatar answered Oct 16 '22 04:10

James Shapiro