如何从Google Colab下载多文件/文件夹或上传文件夹至谷歌云端硬盘?
Got it, let's break down how to handle both folder-based downloads and uploads for Google Drive. I’ll use PyDrive for examples since your existing upload code uses that library—if you’re on the official Google API Client, I’ll note quick adjustments where needed.
To download an entire Google Drive folder (including subfolders and all files) to your local machine, you’ll need to recursively traverse the cloud folder structure, mirror it locally, and download each file. Here’s how to do it:
Step-by-Step Code Example
import os from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive # Initialize authentication (pop-up browser for first-time setup) gauth = GoogleAuth() gauth.LocalWebserverAuth() drive = GoogleDrive(gauth) def download_cloud_folder(cloud_folder_id, local_save_path): # Create local folder if it doesn't exist if not os.path.exists(local_save_path): os.makedirs(local_save_path) # Fetch all items in the target cloud folder file_list = drive.ListFile({'q': f"'{cloud_folder_id}' in parents and trashed=false"}).GetList() for item in file_list: # Recursively download subfolders if item['mimeType'] == 'application/vnd.google-apps.folder': sub_local_path = os.path.join(local_save_path, item['title']) download_cloud_folder(item['id'], sub_local_path) # Download individual files else: local_file_path = os.path.join(local_save_path, item['title']) item.GetContentFile(local_file_path) print(f"Downloaded: {local_file_path}") # Replace with your cloud folder ID and local target path target_cloud_folder_id = "YOUR_CLOUD_FOLDER_ID" # Grab from Drive URL: https://drive.google.com/drive/folders/[ID] local_target_path = "./downloaded_cloud_folder" download_cloud_folder(target_cloud_folder_id, local_target_path)
Note for Official Google API Client Users
If you’re using google-api-python-client instead of PyDrive, the core logic stays the same—you’ll just use drive_service.files().list() to fetch items, and drive_service.files().get_media() with a file writer to download content.
To upload a local folder (including subfolders and files) to Google Drive, you’ll need to mirror your local folder structure in Drive, create subfolders as needed, and upload each file to its corresponding cloud location.
Step-by-Step Code Example
import os from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive # Initialize authentication gauth = GoogleAuth() gauth.LocalWebserverAuth() drive = GoogleDrive(gauth) def create_drive_folder(folder_name, parent_folder_id): # Create a subfolder in the specified parent Drive folder new_folder = drive.CreateFile({ 'title': folder_name, 'mimeType': 'application/vnd.google-apps.folder', 'parents': [{'id': parent_folder_id}] }) new_folder.Upload() print(f"Created cloud folder: {folder_name} (ID: {new_folder['id']})") return new_folder['id'] def upload_local_folder(local_folder_path, parent_drive_folder_id): # Traverse all items in the local folder for item_name in os.listdir(local_folder_path): item_full_path = os.path.join(local_folder_path, item_name) # Recursively upload subfolders if os.path.isdir(item_full_path): sub_drive_folder_id = create_drive_folder(item_name, parent_drive_folder_id) upload_local_folder(item_full_path, sub_drive_folder_id) # Upload individual files else: drive_file = drive.CreateFile({ 'title': item_name, 'parents': [{'id': parent_drive_folder_id}] }) drive_file.SetContentFile(item_full_path) drive_file.Upload() print(f"Uploaded: {item_name} (ID: {drive_file['id']})") # Example usage: # Option 1: Create a new root folder in Drive to upload to root_drive_folder_id = create_drive_folder("Local_Uploads", "root") # "root" = your Drive's main directory # Option 2: Use an existing Drive folder ID # root_drive_folder_id = "EXISTING_DRIVE_FOLDER_ID" local_folder_to_upload = "./your_local_folder" upload_local_folder(local_folder_to_upload, root_drive_folder_id)
Quick Tips
- For large files, PyDrive automatically uses chunked uploads, but if you’re using the official API, you’ll want to set a
chunksizeparameter to avoid timeouts. - Ensure your authentication has sufficient permissions (e.g., for shared drives, you’ll need to add extra scopes during auth setup).
内容的提问来源于stack exchange,提问作者tetedp




