python-3.x - 使用 Python 和 PyDrive 在 Google Drive 上成功上传文件,但文件已损坏

标签 python-3.x pydrive

我正在尝试使用 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/

相关文章:

python - 有没有办法在使用 gdrive API 将文件上传到 Google Drive 时重命名文件?

python - pyDrive 给出 FileNotDownloadableError 错误

python - 使用对数刻度注释 seaborn distplot 会引发错误

python - 从紧密循环内部排序算法可视化 : How to pull values to animate the canvas,

python - Argparse 安装在 anaconda 4.3 for windows 中失败,使用 python 3.6(64 位)

python - 如何在每次不征求许可的情况下将文件上传到另一个用户的谷歌云端硬盘?

python - 使用 PyDrive 将大文件上传到 Google Drive

python-3.x - 使用Pydrive按modifiedTime查询文件

python-3.x - 将 matplotlib 图像发送到 pymsteams(无法创建新标签 pymsteams)

python-3.x - ServerSelectionTimeoutError : basicdatabase-w4eg3. mongodb.net:27017: [Errno 11001] getaddrinfo 失败