我想知道是否可以在父文件夹中获取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