python - PyDrive - Google 驱动器 API - 创建文件夹树

标签 python google-api google-drive-api

我正在尝试使用 python 在 google 驱动器上创建文件夹结构。我已经检查了所有文档以及所有类似的 stackoverflow 问题,但它不起作用。

我想创建的文件夹结构类似于

2017 ----06 --------13 --------14

我没有设法在根目录之外的任何其他位置创建文件夹。布局是扁平的,我不明白为什么。

# this method should create a directory tree, from a string like '2017/06/14'
def create_drive_folder(path):
    # this method should create a folder with the given parents
    # at first it is called with root as parent, for the 2017 folder
    # and when the 06 folder should be created it gets [root and the successfully created 2017 folder id]
    def create_drive_folder_level(filename, parents):
        dirs = drive.ListFile(
            {'q': "'{}' in parents and trashed=false and mimeType='application/vnd.google-apps.folder'".format(
                parents[-1]['id'])})

        try:
            # this will give me the parent folder, if it exists
            current = [x for x in list(dirs)[0] if x['title'] == filename][0]
        except HttpError:
            current = None
        except IndexError:
            current = None
        if not current:
            meta = {'title': filename, 'parents': [x['id'] for x in [parents[-1]],
                    'mimeType': 'application/vnd.google-apps.folder'}
            current = drive.CreateFile(meta)
            current.Upload({'convert': True})
            return current
        return current

    path = path.split('/')
    p = [dict(id='root')]
    for i in range(len(path)):
        p.append(create_drive_folder_level(path[i], p))

create_drive_folder('2017/06/14')

更新:

此代码产生的输出:

{'title': '2017', 'parents': ['root'], 'mimeType': 'application/vnd.google-apps.folder'}
GoogleDriveFile({'title': '2017', 'parents': [{'kind': 'drive#parentReference', 'id': '0AC4TRMtjeM-PUk9PVA', 'isRoot': True}], 'mimeType': 'application/vnd.google-apps.folder', 'kind': 'drive#file', 'id': '0By4TRMtjeM-PRjNPT0pXaFpxY2s', ...})

{'title': '06', 'parents': ['0By4TRMtjeM-PRjNPT0pXaFpxY2s'], 'mimeType': 'application/vnd.google-apps.folder'}
GoogleDriveFile({'title': '06', 'parents': [{'kind': 'drive#parentReference', 'id': '0AC4TRMtjeM-PUk9PVA', 'isRoot': True}], 'mimeType': 'application/vnd.google-apps.folder', 'kind': 'drive#file', 'id': '0By4TRMtjeM-PQUs0OWZ0VFlLTmM', ...})

{'title': '14', 'parents': ['0By4TRMtjeM-PQUs0OWZ0VFlLTmM'], 'mimeType': 'application/vnd.google-apps.folder'}
GoogleDriveFile({'title': '14', 'parents': [{'kind': 'drive#parentReference', 'id': '0AC4TRMtjeM-PUk9PVA', 'isRoot': True}], 'mimeType': 'application/vnd.google-apps.folder', 'kind': 'drive#file', 'id': '0By4TRMtjeM-PN2o4MVplZERiUzA', ...})

在我看来,在创建子目录时我给出了正确的 ID。布局仍然变得扁平。为什么?

最佳答案

我实际上设法找到了解决方案。问题是, parent 必须是字典,而不仅仅是字符串 ID。 所以代替:

{'标题': '06', ' parent ': ['0By4TRMtjeM-PRjNPT0pXaFpxY2s']}

应该是:

{'title': '06', 'parents': [{'id': '0By4TRMtjeM-PRjNPT0pXaFpxY2s'}]}

关于python - PyDrive - Google 驱动器 API - 创建文件夹树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44524537/

相关文章:

python - GAE - 包括外部 python 模块而不将它们添加到存储库?

python - pandas 在 groupby sum 之后对每组内的值进行排序,并在使用 cumsum 后获取值的百分比

ruby-on-rails - 如何在 Ruby on Rails 服务器上验证从 Android 发送的 Google token ID?

google-apps-script - DriveApp 从 DocX 到 PDF 的转换失败

google-drive-api - 无法在 API 控制台中向 Google Drive 注册应用程序

python - 将一个 python 字典中的键映射到另一个字典的确切值

python - 从 python 中的 datetime.timedelta 对象获取时间时出错

python - 尽管添加了正确的范围,但带有 Python 的 Google Drive API 仍不允许文件下载

javascript - 在 WordPress 登录屏幕中实现 reCAPTCHA v3

javascript - 为什么我的 Google Picker 窗口不会出现在浏览器中