gpt4 book ai didi

python-3.x - 使用 Python 和 PyDrive 在谷歌驱动器上成功上传文件,但文件已损坏

转载 作者:行者123 更新时间:2023-12-04 10:55:48 25 4
gpt4 key购买 nike

我正在尝试使用 Python(Pydrive)将文件从我的计算机上传到谷歌驱动器,它成功上传但被损坏并且其大小变为 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')

我做错了什么?我已经完成了身份验证部分,并且已成功通过身份验证。

我已经检查了与此相关的答案,但没有得到我正在寻找的答案。

最佳答案

  • 您要上传 C:\\Users\\New\\Pictures\\Screenshots\\older\\Screenshot.png' 的文件使用 pydrive 和 python 到 Google Drive。
  • 您已经可以使用 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()

    引用:
  • Upload and update file content
  • SetContentFile(filename)

  • 如果这不是您想要的方向,我深表歉意。

    关于python-3.x - 使用 Python 和 PyDrive 在谷歌驱动器上成功上传文件,但文件已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59207524/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com