从父文件列表Google Drive SDK列出子文件

时间:2018-01-31 17:43:00

标签: python google-drive-api

我想知道是否可以在父文件夹中获取list个子文件,如下所示。我找到包含某些搜索条件的父文件夹,然后当这些文件夹位于list时,我会循环查看这些文件夹中的所有文件。问题是在folder_id中使用变量files.list()输入string "folder_id",而不是实际folder id。当我手动输入id时,它运行得很好。

def get_file_ids(service, search=None, max_files=250):

folder_ids= {}
file_ids = {}
response = service.files().list(q="mimeType='application/vnd.google-apps.folder'",
                                     spaces='drive',
                                     fields='nextPageToken, files(id, name)').execute()
items = response.get('files', [])     

if not items:
    print('No files found.')
    return None


for item in items:
    #print('{0} ({1})'.format(item['name'], item['id']))
    if search is None or item['name'].find(search) > -1:
        folder_ids[item['id']]=item['id']        

for folder_id in folder_ids:
    children = service.files().list(q="'folder_id' in parent", fields='nextPageToken, files(id, name)').execute()

    for child in children.get('files', []):
        file_ids[child['name']]=child['id'] 


return file_ids

1 个答案:

答案 0 :(得分:0)

子查询不包括文件夹ID。查询如下所示:

'folder_id' in parent

应该是这样的:

'12312' in parents

尝试updating孩子query以包含文件夹ID,例如:

query = "'{}' in parents".format(folder_id)
children = service.files().list(q=query, 
                                fields='nextPageToken, files(id, name)').execute()