在Python中列出所有Google驱动器文件和文件夹并保存ID
我正试图写一个程序来复制一个文件夹和所有内容,包括子文件夹等到另一个文件夹 我可能过于复杂了,但我觉得第一步是获取所有与它们相关联的文件名和ID,并将它们保存到两个列表中——一个用于文件,一个用于文件夹 我很难让我的程序递归地遍历所有子文件夹,我认为for循环可以使用I从正在填充的列表中选择索引 从下面的输出可以看出,我的程序正在遍历传递给函数的目录和第一个子文件夹,但程序很好地退出了 为大量的代码道歉,但我想上下文很重要 输入:在Python中列出所有Google驱动器文件和文件夹并保存ID,python,python-3.x,google-drive-api,Python,Python 3.x,Google Drive Api,我正试图写一个程序来复制一个文件夹和所有内容,包括子文件夹等到另一个文件夹 我可能过于复杂了,但我觉得第一步是获取所有与它们相关联的文件名和ID,并将它们保存到两个列表中——一个用于文件,一个用于文件夹 我很难让我的程序递归地遍历所有子文件夹,我认为for循环可以使用I从正在填充的列表中选择索引 从下面的输出可以看出,我的程序正在遍历传递给函数的目录和第一个子文件夹,但程序很好地退出了 为大量的代码道歉,但我想上下文很重要 输入: listofdictFolders = [] listofdic
listofdictFolders = []
listofdictFiles = []
def mapFolderContents(folderid):
# retrieves parent name from folderid
parentfolder = service.files().get(fileId=folderid).execute()
parentname = 'Title: %s' % parentfolder['name']
# sets query as argument passed to function, searches for mimeType matching folders and saves to variable
folderquery = "'" + folderid + "'" + " in parents and mimeType='application/vnd.google-apps.folder'"
childrenFoldersDict = service.files().list(q=folderquery,
spaces='drive',
fields='files(id, name)').execute()
# sets query as argument passed to function, searches for mimeType matching NOT folders and saves to variable
notfolderquery = "'" + folderid + "'" + \
" in parents and not mimeType='application/vnd.google-apps.folder'"
childrenFilesDict = service.files().list(q=notfolderquery,
spaces='drive',
fields='files(name, id)').execute()
# takes value pair of 'files' which is a list of dictionaries containing ID's and names.
childrenFolders = (childrenFoldersDict['files'])
# takes value pair of 'files' which is a list of dictionaries containing ID's and names.
childrenFiles = (childrenFilesDict['files'])
# if no files found, doesn't append to list
if len(childrenFiles) > 0:
listofdictFiles.append(['Parent Folder ' + parentname, childrenFiles])
# if no folders found, doesn't append to list
if len(childrenFolders) > 0:
listofdictFolders.append(['Parent Folder ' + parentname, childrenFolders])
# finds length of list for use in for loop later to avoid index out of range error
maxIndex = len(listofdictFolders)
# for loop to find ID's and names of folders returned above and append name and ID's to dictionary
for i in range(0, maxIndex):
# strip variables are to access ID values contained in dictionary
strip1 = listofdictFolders[0]
strip2 = strip1[1]
print('Now indexing ' + str(strip2[i]['name']) + '...')
# saves query to variable using strip2 variable, index and 'id' key
loopquery = "'" + str(strip2[i]['id']) + "'" \
+ " in parents and mimeType='application/vnd.google-apps.folder'"
loopquery2 = "'" + str(strip2[i]['id']) + "'" \
+ " in parents and not mimeType='application/vnd.google-apps.folder'"
# saves return value (dictionary) to variable
loopreturn = service.files().list(q=loopquery,
spaces='drive',
fields='files(id, name)').execute()
loopreturn2 = service.files().list(q=loopquery2,
spaces='drive',
fields='files(id, name)').execute()
loopappend = (loopreturn['files'])
loopappend2 = (loopreturn2['files'])
# appends list of dictionaries to listofdictFolders
listofdictFolders.append(['Parent Folder Title: ' + str(strip2[i]['name']), loopappend])
listofdictFiles.append(['Parent Folder Title: ' + str(strip2[i]['name']), loopappend2])
mapFolderContents(blankJobFolderID)
pprint.pprint(listofdictFiles)
print('')
pprint.pprint(listofdictFolders)
输出:
Now indexing subfolder 1...
[['Parent Folder Title: Root',
[{'id': 'subfolder 1 ID', 'name': 'subfolder 1'},
{'id': 'subfolder 2 ID', 'name': 'subfolder 2'},
{'id': 'subfolder 3 ID', 'name': 'subfolder 3'}]],
['Parent Folder Title: subfolder 1',
[{'id': 'sub-subfolder1 ID', 'name': 'sub-subfolder 1'},
{'id': 'sub-subfolder2 ID', 'name': 'sub-subfolder 2'}]]]
[['Parent Folder Title: Venue',
[{'id': 'sub-file 1 ID',
'name': 'sub-file 1'}]]]
Process finished with exit code 0
您可以使用递归BFS来检索所有文件和文件夹
以下是我的方法:
def getChildrenFoldersByFolderId(folderid):
folderquery = "'" + folderid + "'" + " in parents and mimeType='application/vnd.google-apps.folder'"
childrenFoldersDict = service.files().list(q=folderquery,
spaces='drive',
fields='files(id, name)').execute()
return childrenFoldersDict['files']
def getChildrenFilesById(folderid):
notfolderquery = "'" + folderid + "'" + \
" in parents and not mimeType='application/vnd.google-apps.folder'"
childrenFilesDict = service.files().list(q=notfolderquery,
spaces='drive',
fields='files(name, id)').execute()
return childrenFilesDict['files']
def getParentName(folderid):
# retrieves parent name from folderid
parentfolder = service.files().get(fileId=folderid).execute()
parentname = 'Title: %s' % parentfolder['name']
return parentname
def bfsFolders(queue=[]):
listFilesFolders = {}
while len(queue) > 0:
currentFolder = queue.pop()
childrenFolders = getChildrenFoldersByFolderId(currentFolder['id'])
childrenFiles = getChildrenFilesById(currentFolder['id'])
parentName = getParentName(currentFolder['id'])
listFilesFolders['folderName'] = currentFolder['name']
listFilesFolders['folderId'] = currentFolder['id']
listFilesFolders['parentName'] = parentName
if len(childrenFiles) > 0:
listFilesFolders['childrenFiles'] = childrenFiles
if len(childrenFolders) <= 0:
return listFilesFolders
listFilesFolders['childrenFolders'] = []
for child in childrenFolders:
queue.append(child)
listFilesFolders['childrenFolders'].append(bfsFolders(queue))
return listFilesFolders
filesAndFolders = bfsFolders([{'id': "ASDASDASDASDVppeC1zVVlWdDhkASDASDQ", 'name': 'folderRoot'}])
pprint.pprint(filesAndFolders)
首先,分离函数以简化脚本。完成此操作后,请使用BFS,方法是使用根节点作为参数,其中包含文件夹的ID和名称
宽度优先递归搜索将使用名为ListFileFolders的FIFO列表,其中包含一个字典。一旦设置了字典,它将返回节点字典本身,除非有更多的文件夹要扩展。作为一个方向,我使用python库提出了一个答案。你能确认一下吗?如果这不是你期望的方向,我道歉。谢谢你的回复。给您带来不便,我深表歉意。不幸的是,我无法复制你的情况。当我测试这个脚本时,可以获得具有文件夹结构的文件列表。对此我深表歉意。这是因为我的技术差。我再次为此深表歉意。因此,从你的答复中,我了解到我提出的答案对你的情况没有用处。所以我想删除这个。因为我不想混淆其他用户。我再次为我的拙劣技能深表歉意。我将继续按照你的建议工作,我目前正在查看源代码,因为根据输出结果,这正是我想要的,但有些东西不符合我的目的。不需要道歉,我感谢你的回复和帮助。如果我让它工作,我会让你知道。谢谢你的回复。此外,我正在测试以复制您的情况。因此,当我能够正确地复制您的情况并找到您问题的原因时,我希望通过包含信息重新打开我的答案。但是,现在,我不得不为我拙劣的技术道歉。我想多学习。