使用pydrive从google共享驱动器删除文件,文件存在,但API返回404未找到文件错误

2024-10-01 09:40:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用pydrive删除共享驱动器上的文件,但无法删除该文件。 当我获取驱动器上文件的信息时,它会显示文件ID,但当我使用“Trash()函数”时,它会显示一个错误为“file not found:XXX”

开发环境

ubuntu 20.04 on docker
python 3.7
pydrive 1.3.1

代码内容

from pydrive.auth import GoogleAuth
from oauth2client.service_account import ServiceAccountCredentials
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
scope = ['https://www.googleapis.com/auth/drive']
gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name("client_secrets.json", scope)
drive = GoogleDrive(gauth)

for f in drive.ListFile({'driveId': 'AAA', 'corpora': 'drive', 'includeItemsFromAllDrives': 'true',
 'supportsAllDrives': 'true', 'q': 'title = "lock" and trashed = false'}).GetList():
    print(f['title'], '  \t', f['id'])

    f_txt = drive.CreateFile(f)
    f_txt.Trash()

错误内容

root@test:~/reman/src/For_lambda/refer# python3 delete_lockfile.py 
lock     10xM-FIkMoyRIb7PC1VBxZZJn_QePxy-I
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/pydrive/files.py", line 410, in _FilesTrash
    http=self.http)
  File "/usr/local/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/googleapiclient/http.py", line 915, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError:
 <HttpError 404 when requesting https://www.googleapis.com/drive/v2/files/10xM-FIkMoyRIb7PC1VBxZZJn_QePxy-I/trash?alt=json returned 
"File not found: 10xM-FIkMoyRIb7PC1VBxZZJn_QePxy-I". 
Details: "File not found: 10xM-FIkMoyRIb7PC1VBxZZJn_QePxy-I">

如上所示,我能够获取[fileid:10xM-FIkMoyRIb7PC1VBxZZJn_QePxy-I],但当我删除该文件时,它显示为未找到

我不知道发生了什么事。请帮忙!谢谢


Tags: 文件infrompyimportjsonusrnot
1条回答
网友
1楼 · 发布于 2024-10-01 09:40:45

看起来你没有上传文件。使用drive.CreateFile(f)可以创建一个GoogleDriveFile实例,但必须使用Upload()方法来创建。这就是它向您显示File not found异常的原因。您可以在for中尝试以下内容:

    f_txt = drive.CreateFile(f)
    f_txt.Upload()
    f_txt.Trash()

相关问题 更多 >