簡體   English   中英

使用python的谷歌驅動器文件夾中的文件列表

[英]List of files in a google drive folder with python

我有與這篇文章中提出的完全相同的問題: 列出 google drive 文件夾中的文件和文件夾我在 google drive rest api 文檔中沒有弄清楚如何獲取 google 文件夾中的文件列表駕駛

您可以在此處查看有關如何在雲端硬盤中列出文件的示例: https : //developers.google.com/drive/api/v3/search-files 您需要構建一個列出文件夾中文件的查詢:使用

q = "'1234' in parents"

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

這是一個hacky但成功的解決方案。 這實際上從特定的 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)

有關可用功能,請參閱API ...

您可以使用 Drive API files: list 方法搜索文件。 您可以不帶任何參數調用 Files.list,它會返回用戶驅動器上的所有文件。 默認情況下, Files.list 只返回資源的屬性子集。 如果您希望返回更多屬性,請使用 fields 參數指定要在查詢字符串 q 中返回的屬性。 為了使您的搜索查詢更加具體,您可以對每個查詢屬性使用多個運算符。

# 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']))


如果您正在使用谷歌合作,最簡單的解決方案。

在協作筆記本中連接到您的雲端硬盤:

    from google.colab import drive
    drive.mount('/content/drive')

使用特殊命令“!” 使用“ls”命令查看您指定的文件夾驅動器路徑中的文件列表。

    !ls PATH OF YOUR DRIVE FOLDER

示例:!ls drive/MyDrive/Folder1/Folder2/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM