【问题标题】:How to read all of the files in a specific directory in google drive using python?如何使用python读取谷歌驱动器中特定目录中的所有文件?
【发布时间】:2019-07-18 18:02:12
【问题描述】:

我正在尝试浏览文件夹中的所有文件以在 python 中读取和分析它们。这些文件有很多,我想遍历文件夹中的每个文件,而不必硬编码其中的文件名。

drive = GoogleDrive(gauth)
file_list = drive.ListFile({'q': "'1fasSmc7insM4cQOoCik0vmEvYLN6Z5XZ' in parents and trashed=false"}).GetList()



---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/oauth2client/clientsecrets.py in _loadfile(filename)
    120     try:
--> 121         with open(filename, 'r') as fp:
    122             obj = json.load(fp)

FileNotFoundError: [Errno 2] No such file or directory: 'client_secrets.json'

During handling of the above exception, another exception occurred:

InvalidClientSecretsError                 Traceback (most recent call last)
10 frames
InvalidClientSecretsError: ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)

During handling of the above exception, another exception occurred:

InvalidConfigError                        Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pydrive/auth.py in LoadClientConfigFile(self, client_config_file)
    386       client_type, client_info = clientsecrets.loadfile(client_config_file)
    387     except clientsecrets.InvalidClientSecretsError as error:
--> 388       raise InvalidConfigError('Invalid client secrets file %s' % error)
    389     if not client_type in (clientsecrets.TYPE_WEB,
    390                            clientsecrets.TYPE_INSTALLED):

InvalidConfigError: Invalid client secrets file ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)

【问题讨论】:

标签: google-drive-api pydrive


【解决方案1】:

要在根目录中搜索,可以使用:

file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()

这会为您提供根目录中所有文件的列表。

要获得子目录中所有文件的列表,您可以将“root”替换为文件夹 ID。

folder_id = '****'
file_list = drive.ListFile({'q': "'folder_id' in parents and trashed=false"}).GetList()

【讨论】:

    猜你喜欢
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    • 2020-05-21
    相关资源
    最近更新 更多