【问题标题】:Trying to get all file IDs in folders in Google Drive尝试获取 Google Drive 文件夹中的所有文件 ID
【发布时间】:2019-06-04 08:55:22
【问题描述】:

我正在尝试使用 Python 从 Drive 中提取所有文件 ID,在 Wing IDE 中构建我的代码,但我无法从文件夹中的文件中提取文件 ID。

我在网上找不到任何解决方案,因此发布此信息希望得到一些帮助。

我是编程爱好者,所以我真的不知道从哪里开始。

任何帮助将不胜感激,谢谢!

【问题讨论】:

标签: python google-api google-drive-api google-api-python-client


【解决方案1】:

您需要做的是在files.list 方法中使用Q 参数。通过添加以下内容,它将请求所有不是目录的文件

q=mimeType+!%3D+%22application%2Fvnd.google-apps.folder%22

然后你可以添加fields参数只返回文件的id

fields=files%2Fid

完整的请求请求如下所示

GET https://www.googleapis.com/drive/v3/files?q=mimeType+!%3D+%22application%2Fvnd.google-apps.folder%22&fields=files%2Fid&key={YOUR_API_KEY}

至于 python 部分,我建议您遵循此示例Python quickstart,一旦您了解了它的工作原理,您就可以开始将 API 与 python 一起使用,您可以继续向其中添加 q 和 fields 参数。

# Call the Drive v3 API
results = service.files().list(
    pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])

【讨论】:

    【解决方案2】:

    有一种更简单的方法更有意义。 安装 Python 和 Gam 后,您可以运行一个脚本,该脚本使用来自 google 驱动器的文件 id 在 csv 文件中导出列表中的所有文档。安装 python 和 gam 后,您需要安装一些模块才能使脚本正常工作。当您运行脚本时,可以搜索错误代码以查看需要在 Python 中安装的内容。此外,您还需要创建一个 api 凭据服务帐户,并在两个地方的脚本中将该帐户名称替换为该帐户名称。 以管理员身份使用以下命令运行 cmd,脚本名称为 script.py。 "C:\Users\dcahoon\AppData\Local\Programs\Python\Python38\python.exe C:\GAM\SCRIPT.PY **脚本开始

    import os
    import subprocess
    
    from csv import writer
    from csv import reader
    
    # path to googleidlist.csv
    csvfile = 'c:\\GAM\\googleidlist.csv'
    destination = 'c:\\GAM\\OUTPUT\\'      #Destination for downloaded documents
    
    
    # Open the input_file in read mode and output_file in write mode
    with open(csvfile, 'r') as read_obj, \
            open('output_1.txt', 'w', newline='') as write_obj:
        # Create a csv.reader object from the input file object
        csv_reader = reader(read_obj)
        # Create a csv.writer object from the output file object
        csv_writer = writer(write_obj)
        # Read each row of the input csv file as list
        for row in csv_reader:
             file_id = row[0]
            outcome = subprocess.Popen(['gam', 'user', 'googleserviceaccountname', 'get', 'drivefile', 'id', file_id, 'targetfolder',destination], stdout=subprocess.PIPE)
            # os.system("gam user david.bruinsma@colonialmed.com show fileinfo "+ file_id + "name")
            filename = subprocess.Popen(['gam', 'user', 'googleserviceaccountname', 'show', 'fileinfo', file_id, 'name' ], stdout=subprocess.PIPE)
            output = outcome.stdout.readline()
            file_name = filename.stdout.readline()
            print(output)
            # Append the default text in the row / list
            # row.append(filename)
            row.append(output)
            row.append(file_name)
            row.append(file_id)
    
            # Add the updated row / list to the output file
            csv_writer.writerow(row)
    

    **脚本结束

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多