在 JupyterLab 中读取 Google Drive 中的文件可以通过多种方法实现。以下是几种常见的方法:
pydrive
库pydrive
库:pip install pydrive
pydrive
读取文件:from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive import os # 设置 OAuth 2.0 凭据文件路径 os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/your/credentials.json" # 认证并创建 GoogleDrive 实例 gauth = GoogleAuth() gauth.LocalWebserverAuth() # Creates local webserver and auto handles authentication. drive = GoogleDrive(gauth) # 列出 Google Drive 中的文件 file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList() for file in file_list: print(f"title: {file['title']}, id: {file['id']}") # 下载文件 file_id = 'your_file_id' downloaded_file = drive.CreateFile({'id': file_id}) downloaded_file.GetContentFile('downloaded_file_name')
google.colab
库(适用于 Google Colab)如果你在 Google Colab 中工作,可以使用 google.colab
库来挂载 Google Drive:
from google.colab import drive drive.mount('/content/drive')
import pandas as pd # 假设文件在 Google Drive 中的路径为 /content/drive/My Drive/your_file.csv file_path = '/content/drive/My Drive/your_file.csv' df = pd.read_csv(file_path) print(df.head())
gdown
库gdown
库可以直接下载 Google Drive 中的文件:
gdown
库:pip install gdown
gdown
下载文件:import gdown # Google Drive 文件的共享链接 url = 'https://drive.google.com/uc?id=your_file_id' output = 'downloaded_file_name' gdown.download(url, output, quiet=False)
google-auth
和 google-api-python-client
库pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client
google-api-python-client
读取文件:from google.oauth2 import service_account from googleapiclient.discovery import build import io from googleapiclient.http import MediaIoBaseDownload # 设置 OAuth 2.0 凭据文件路径 SCOPES = ['https://www.googleapis.com/auth/drive.readonly'] SERVICE_ACCOUNT_FILE = 'path/to/your/credentials.json' credentials = service_account.Credentials.from_service_account_file( SERVICE_ACCOUNT_FILE, scopes=SCOPES) # 创建 Google Drive API 客户端 service = build('drive', 'v3', credentials=credentials) # 列出 Google Drive 中的文件 results = service.files().list(pageSize=10, fields="nextPageToken, files(id, name)").execute() items = results.get('files', []) if not items: print('No files found.') else: print('Files:') for item in items: print(f"{item['name']} ({item['id']})") # 下载文件 file_id = 'your_file_id' request = service.files().get_media(fileId=file_id) fh = io.FileIO('downloaded_file_name', 'wb') downloader = MediaIoBaseDownload(fh, request) done = False while done is False: status, done = downloader.next_chunk() print(f"Download {int(status.progress() * 100)}%.") fh.close()
通过以上方法,你可以在 JupyterLab 中读取 Google Drive 中的文件。选择适合你需求的方法进行操作。
领取专属 10元无门槛券
手把手带您无忧上云