Skip to main content
 首页 » 编程设计

python之无效查询,Drive api,files.list()

2025年05月04日21Leo_wl

这个功能:

drive.ListFile({'q': " id = '"+Value+"' and trashed=false"}).GetList() 

返回:“无效查询”

问题出在选择器“id”上,如果我提出这个条件,我会返回一个带有参数“id”的字典

我用过这个文档, https://developers.google.com/drive/v2/reference/files/list 和 https://developers.google.com/drive/v3/web/search-parameters

它适用于其他选择器,但不适合“id”

请您参考如下方法:

编辑:这里是您如何访问元数据,以及如何通过其 ID 获取和设置现有文件的内容。

# Calling CreateFile() with an existing ID links it to the already existing file. 
gdrive_file = drive.CreateFile({'id': '<your file id>'}) 
gdrive_file.FetchMetadata() # Only needed if you want to view or edit its metadata. 
# Do things with metadata here as needed. 
 
gdrive_file.GetContentFile('dog.png') # Download content file. 
 
# And/or upload a new content file. 
gdrive_file.SetContentFile('cat.png')  
gdrive_file.Upload() 

当然还有 docs有很多例子。

原创: 看看 docs for file listings例如。

确保你

  1. 不要在 = 符号周围引入您自己的空格,
  2. PyDrive 使用 Google Drive API v2,因此请确保使用 the v2 search parameter page相反,
  3. ID是Google Drive分配的文件夹ID,this SO question列出了查找 ID 的方法。

例如:

from pydrive.drive import GoogleDrive 
 
drive = GoogleDrive(gauth) # Create GoogleDrive instance with authenticated GoogleAuth instance 
folder_id = "insert your folder ID here" 
 
# Auto-iterate through all files in the specified folder. 
file_list = drive.ListFile({ 
    'q': "{id} in parents and trashed=false".format(id=folder_id) 
}).GetList() 
 
for file1 in file_list: 
  print('title: %s, id: %s' % (file1['title'], file1['id']))