列出谷歌驱动器中的文件/文件夹,包括子文件夹等......使用谷歌驱动器api

时间:2021-06-24 11:11:31

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

我有一个 google drive 文件夹,里面有很多文件夹,然后这些文件夹里面还有其他文件夹/文件等等......我正在使用这个代码列出父文件夹中的所有文件夹/文件,我我将文件/文件夹放在这样的数据框中:

import pickle
import os.path
import io
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from numpy import cumproduct
import pandas as pd
import gdown
from pyasn1.type.constraint import ContainedSubtypeConstraint
import requests
from googleapiclient.http import MediaIoBaseDownload
import httplib2

SCOPES = ['https://www.googleapis.com/auth/drive']

creds = None
if os.path.exists('token.pickle'):
    with open('token.pickle', 'rb') as token:
        creds = pickle.load(token)
if not creds or not creds.valid:
    if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
    else:
        flow = InstalledAppFlow.from_client_secrets_file(
                'test.json', SCOPES)
        creds = flow.run_local_server(port=0)
    with open('token.pickle', 'wb') as token:
        pickle.dump(creds, token)

service = build('drive', 'v3', credentials=creds)

folder_id='valid folder id'
query=f"parents = '{folder_id}'"

response=service.files().list(pageSize=1000, q=query).execute()
files=response.get('files')
nextPageToken=response.get('nextPageToken')

while nextPageToken:
    response=service.files().list(pageSize=1000, pageToken=nextPageToken, q=query).execute()
    files.extend(response.get('files'))
    nextPageToken=response.get('nextPageToken')

df = pd.DataFrame(files)
print(df)

这是我得到的输出:

          kind            id               name                        mimeType
0        drive#file    valid id          Testfolder           application/vnd.google-apps.folder
.           .             .                   .                            .
.           .             .                   .                            .
.           .             .                   .                            .
2000   drive#file    valid id          Testfolder2000         application/vnd.google-apps.folder

如何修改此代码以同时列出子文件夹中的所有文件? 假设父文件夹名称是 root 并且我在此路径中有文件:root/folder1/folder2/folder.../test.png。所以,我想要的输出是在一个数据框中包含所有文件/文件夹。

0 个答案:

没有答案