【问题标题】:How can I download specific files from Google drive programmatically using Python? [duplicate]如何使用 python 以编程方式从谷歌驱动器下载特定文件
【发布时间】:2020-08-13 09:54:09
【问题描述】:

我的谷歌驱动器的不同文件夹中有大约 100k 个文件。我想从中下载特定文件。谷歌驱动器中文件的路径在 csv 中。

但是我怎样才能得到文件的 id 呢?我尝试了以下方法。

import pandas as pd
from apiclient import errors
#from pygdrive3 import service


def retrieve_all_files(service):
  """Retrieve a list of File resources.

  Args:
    service: Drive API service instance.
  Returns:
    List of File resources.
  """
  result = []
  page_token = None
  while True:
    try:
      param = {}
      if page_token:
        param['pageToken'] = page_token
      files = service.files().list(**param).execute()

      #result.extend(files['items'])
      idval = files.get('id')
      if not idval:
        break
    except errors.HttpError.error:
      print ('An error occurred: %s' % error)
      break
  return idval


df = pd.read_csv("/home/ram/Downloads/Data_Science/Kaggle Competition/BBox_List_2017_path_colab.csv",header=None)
print(df.head())
for i in df[0]:
    request = drive_service.files()
    result = retrieve_all_files(request)
    fh = io.BytesIO()
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print ("Download %d%%." % int(status.progress() * 100))

但错误显示,drive_service is not defined。下面是我的 csv

                                                   0           1  ...           4            5
0  /content/drive/My Drive/nihxray/images_001/ima...  225.084746  ...   79.186441  Atelectasis
1  /content/drive/My Drive/nihxray/images_001/ima...  686.101695  ...  313.491525  Atelectasis
2  /content/drive/My Drive/nihxray/images_001/ima...  221.830508  ...  216.949153  Atelectasis
3  /content/drive/My Drive/nihxray/images_001/ima...  726.237288  ...   55.322034  Atelectasis
4  /content/drive/My Drive/nihxray/images_001/ima...  660.067797  ...   78.101695  Atelectasis

我只下载了上面 csv 中的那些文件。我怎样才能在 python 中做到这一点。感谢任何帮助

【问题讨论】:

  • 为了使用drive_service,您必须首先按照here 的说明构建它。您的函数需要 service 作为参数,drive_service 未在代码中的任何位置定义。
  • 我确实这样做了,但是我没有收到任何可以在新浏览器中打开的 URL!正如运行 python quckstart.py 时解释的那样
  • 代码运行成功后,您是否在控制台中获得了授权URL链接?然后按照here的说明手动复制粘贴到浏览器中?
  • 这就是我说的,我没听懂!我为桌面而不是网络浏览器启用了驱动器 api。它是 bcos 吗?
  • 但是你能运行你的代码没有错误吗?

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


【解决方案1】:

这里有两个来自异步 Google API 客户端的 sn-ps,这可能更适合您,因为它可以让您同时下载多个文件:

列出文件(按 ID):https://github.com/omarryhan/aiogoogle/blob/master/examples/list_drive_files.py

下载文件:https://github.com/omarryhan/aiogoogle/blob/master/examples/download_drive_file.py

【讨论】:

    【解决方案2】:

    有一种更简单的方法更有意义。安装 Python 和 Gam 后,您可以运行一个脚本,该脚本使用 csv 文件中来自谷歌驱动器的文件 id 来导出列表中的所有文档。安装 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
      • 2018-01-12
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多