无法上传文件到Google Drive

1 投票
2 回答
1115 浏览
提问于 2025-04-18 18:55

我想通过程序把我电脑里的一个文件上传到Google Drive,代码是这样的:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
drive = GoogleDrive(gauth)
this_file = 'apple.txt'
this_file.Upload()

但是,我遇到了以下错误:

AttributeError: 'str' object has no attribute 'Upload'

我该怎么上传呢?

2 个回答

0

你正在尝试上传一个字符串。你需要先创建一个文件流,然后在这个文件流上调用上传的方法。

this_file = open("apple.txt","r")
this_file.Upload() 
2

这个例子在这里提到要这样做:

this_file = drive.CreateFile()
this_file.SetContentFile('apple.txt') # Read file and set it as a content of this instance.
this_file.Upload() # Upload it

撰写回答