我正在尝试使用 Python(Pydrive) 将文件从我的计算机上传到 google 云端硬盘,文件上传成功,但遭到损坏,大小变为 0kb。
这是我的代码:
drive = GoogleDrive(gauth)
import glob, os
with open('C:\\Users\\New\\Pictures\\Screenshots\\older\\Screenshot.png', 'r') as file:
fn = os.path.basename(file.name)
file_drive = drive.CreateFile({"title": fn })
file_drive.Upload()
print('The file: ' + fn + ' has been uploaded')
print('Successful')
我做错了什么?我已经完成了身份验证部分,并且已成功身份验证。
我已经检查了与此相关的答案,但没有得到我正在寻找的答案。
最佳答案
- 您想要使用 pydrive 和 python 将
C:\\Users\\New\\Pictures\\Screenshots\\older\\Screenshot.png'
文件上传到 Google 云端硬盘。< - 您已经能够使用 Drive API。
如果我的理解是正确的,那么修改一下怎么样?
修改点:
- 在您的脚本中,未给出文件内容。
修改后的脚本:
请按如下方式修改您的脚本。
从:with open('C:\\Users\\New\\Pictures\\Screenshots\\older\\Screenshot.png', 'r') as file:
fn = os.path.basename(file.name)
file_drive = drive.CreateFile({"title": fn })
file_drive.Upload()
到:
f = 'C:\\Users\\New\\Pictures\\Screenshots\\older\\Screenshot.png' # Added
with open(f, 'r') as file: # Modified
fn = os.path.basename(file.name)
file_drive = drive.CreateFile({"title": fn })
file_drive.SetContentFile(f) # Added
file_drive.Upload()
引用文献:
如果这不是您想要的方向,我深表歉意。
关于python-3.x - 使用 Python 和 PyDrive 在 Google Drive 上成功上传文件,但文件已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59207524/