我如何使用python以编程方式从Google驱动器下载特定文件

问题描述 投票:0回答:1

我在Google驱动器的不同文件夹中有大约10万个文件。我要从中下载特定文件。Google驱动器中文件的路径在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中进行安装?感谢您的帮助

python csv google-drive-api google-api-python-client downloadfile
1个回答
0
投票

这里是来自异步Google API客户端的两个摘要,由于您可以同时下载多个文件,因此这可能更适合您:

列出文件(按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

© www.soinside.com 2019 - 2024. All rights reserved.