如何用Python上传本地文件夹至Google Drive?权限问题求助
解决PyDrive上传本地文件夹到Google Drive的权限错误及层级上传问题
问题说明
需要将包含子文件夹与文件层级结构的本地文件夹(示例路径:C:\Users\test\Documents\google drvie\test\upload)上传至Google Drive,但现有PyDrive脚本仅能上传文件,上传文件夹时触发PermissionError权限拒绝错误。
报错信息
GoogleDriveFile({'parents': [{'id': '1J8TXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'}], 'title': 'upload'}) Traceback (most recent call last): File "c:\Users\test\Documents\google drvie\googledrive_file_up.py", line 43, in <module> f.SetContentFile(os.path.join(path,x)) File "C:\Users\test\AppData\Roaming\Python\Python39\site-packages\pydrive\files.py", line 169, in SetContentFile self.content = open(filename, 'rb') PermissionError: [Errno 13] Permission denied: 'C:\\Users\\test\\Documents\\google drvie\\test\\upload'
现有代码
from pydrive.drive import GoogleDrive from pydrive.auth import GoogleAuth import os #Authenticate Google services gauth = GoogleAuth() # load credentials or create empty credentials if none exist gauth.LoadCredentialsFile("mycreds.txt") #If you don't have Google service credentials if gauth.credentials is None: #Automatically receive authorization code from user and configure local web server gauth.LocalWebserverAuth() # if the access token does not exist or has expired elif gauth.access_token_expired: #refresh authorization for google services gauth.Refresh() # if none match else: #Authorize Google services gauth.Authorize() # save credentials to file in txt format gauth.SaveCredentialsFile("mycreds.txt") #Authentication process for Google Drive drive = GoogleDrive(gauth) # specify folder path to upload path = r'C:\Users\test\Documents\google drvie\test' # File ID to upload to GOOGLE DRIVE folder_id='1J8TXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' #Loop processing (repeated processing) by for statement for x in os.listdir(path): # Create GoogleDriveFile object #f = drive.CreateFile({'title' : x}) f = drive.CreateFile({"parents": [{"id": folder_id},]}) #file title f['title'] = x # set local file and upload print(f) f.SetContentFile(os.path.join(path,x)) print(f) #Upload to Google Drive f.Upload() print(f) f = None
解决方案
错误根源是os.listdir()会同时列出文件和文件夹,而SetContentFile()只能处理文件,尝试打开文件夹会触发权限拒绝。需要递归遍历本地文件夹结构,区分文件和文件夹分别处理:
修改后完整代码
from pydrive.drive import GoogleDrive from pydrive.auth import GoogleAuth import os # 认证Google服务 gauth = GoogleAuth() gauth.LoadCredentialsFile("mycreds.txt") if gauth.credentials is None: gauth.LocalWebserverAuth() elif gauth.access_token_expired: gauth.Refresh() else: gauth.Authorize() gauth.SaveCredentialsFile("mycreds.txt") drive = GoogleDrive(gauth) def upload_local_folder(local_path, parent_drive_id): # 遍历当前本地路径下的所有内容 for item_name in os.listdir(local_path): item_full_path = os.path.join(local_path, item_name) if os.path.isfile(item_full_path): # 处理文件:上传到指定Google Drive文件夹 file = drive.CreateFile({ 'parents': [{'id': parent_drive_id}], 'title': item_name }) file.SetContentFile(item_full_path) file.Upload() print(f"已上传文件: {item_name}") file = None elif os.path.isdir(item_full_path): # 处理文件夹:先在Google Drive创建同名文件夹,再递归上传内部内容 folder = drive.CreateFile({ 'parents': [{'id': parent_drive_id}], 'title': item_name, 'mimeType': 'application/vnd.google-apps.folder' }) folder.Upload() new_folder_id = folder['id'] print(f"已创建文件夹: {item_name},开始上传内部内容") # 递归上传子文件夹内容 upload_local_folder(item_full_path, new_folder_id) folder = None # 配置上传参数 local_root_path = r'C:\Users\test\Documents\google drvie\test' target_drive_folder_id = '1J8TXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' # 启动上传 upload_local_folder(local_root_path, target_drive_folder_id) print("上传完成")
关键修改说明
- 新增递归函数
upload_local_folder,实现层级遍历上传 - 通过
os.path.isfile和os.path.isdir区分文件与文件夹 - 创建Google Drive文件夹时,必须指定
mimeType为application/vnd.google-apps.folder,否则会被识别为普通文件 - 递归调用时,将新创建的文件夹ID作为父ID传入,保证层级结构一致
内容的提问来源于stack exchange,提问作者user55416
相关产品推荐
相关产品推荐

