【问题标题】:List of files in a google drive folder with python使用python的谷歌驱动器文件夹中的文件列表
【发布时间】:2019-11-13 09:56:24
【问题描述】:

我的问题与这篇文章中提出的问题完全相同:List files and folders in a google drive folder 我在 google drive rest api 文档中没有弄清楚如何获取 google drive 文件夹中的文件列表

【问题讨论】:

标签: python python-3.x google-drive-api


【解决方案1】:

有关可用功能,请参阅API...

您可以使用 Drive API files: list 方法搜索文件。您可以不带任何参数调用 Files.list,它会返回用户驱动器上的所有文件。默认情况下,Files.list 仅返回资源的一部分属性。如果您想要返回更多属性,请使用 fields 参数指定要在查询字符串 q 中返回的属性。为了使您的搜索查询更加具体,您可以对每个查询属性使用多个运算符。

【讨论】:

    【解决方案2】:

    您可以在此处查看如何在云端硬盘中列出文件的示例:https://developers.google.com/drive/api/v3/search-files。您需要构造一个列出文件夹中文件的查询:使用

    q = "'1234' in parents"
    

    其中 1234 是您要列出的文件夹的 ID。您可以修改查询以列出特定类型的所有文件(例如文件夹中的所有jpeg文件)等。

    【讨论】:

    • 如何找出特定文件夹的ID?
    • @JinghuiNiu 您可以在查询中添加 'mimeType = 'application/vnd.google-apps.folder'' (阅读提供的文档链接会有所帮助),现在它只会列出文件夹,通过搜索文件夹的名称找到 id
    【解决方案3】:

    这是一个简单但成功的解决方案。这实际上获取来自特定 Google Drive 文件夹(在本例中为名为“thumbnails”的文件夹)中的所有文件。我需要从特定文件夹中获取(不仅仅是列出)所有文件并对它们进行图像调整,所以我使用了以下代码:

    `# First, get the folder ID by querying by mimeType and name
    folderId = drive.files().list(q = "mimeType = 'application/vnd.google-apps.folder' and name = 'thumbnails'", pageSize=10, fields="nextPageToken, files(id, name)").execute()
    # this gives us a list of all folders with that name
    folderIdResult = folderId.get('files', [])
    # however, we know there is only 1 folder with that name, so we just get the id of the 1st item in the list
    id = folderIdResult[0].get('id')
    
    # Now, using the folder ID gotten above, we get all the files from
    # that particular folder
    results = drive.files().list(q = "'" + id + "' in parents", pageSize=10, fields="nextPageToken, files(id, name)").execute()
    items = results.get('files', [])
    
    # Now we can loop through each file in that folder, and do whatever (in this case, download them and open them as images in OpenCV)
    for f in range(0, len(items)):
        fId = items[f].get('id')
        fileRequest = drive.files().get_media(fileId=fId)
                fh = io.BytesIO()
                downloader = MediaIoBaseDownload(fh, fileRequest)
                done = False
                while done is False:
                    status, done = downloader.next_chunk()
        fh.seek(0)
        fhContents = fh.read()
        
        baseImage = cv2.imdecode(np.fromstring(fhContents, dtype=np.uint8), cv2.IMREAD_COLOR)
    

    【讨论】:

      【解决方案4】:
      # Import PyDrive and associated libraries.
      # This only needs to be done once per notebook.
      from pydrive.auth import GoogleAuth
      from pydrive.drive import GoogleDrive
      from google.colab import auth
      from oauth2client.client import GoogleCredentials
      
      # Authenticate and create the PyDrive client.
      # This only needs to be done once per notebook.
      auth.authenticate_user()
      gauth = GoogleAuth()
      gauth.credentials = GoogleCredentials.get_application_default()
      drive = GoogleDrive(gauth)
      
      # List .txt files in the root.
      #
      # Search query reference:
      # https://developers.google.com/drive/v2/web/search-parameters
      listed = drive.ListFile({'q': "title contains 'CV'"}).GetList()
      for file in listed:
          print('title {}, id {}'.format(file['title'], file['id']))
      
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-21
        • 1970-01-01
        • 2021-10-15
        • 2022-12-18
        • 1970-01-01
        相关资源
        最近更新 更多