编辑:这是您如何访问元数据,并通过其 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()
而且,当然,文档有很多例子。
原文:查看文档以获取文件列表以获取示例。
确保您
- 不要在
=
标志周围引入自己的空间,
- PyDrive 使用 Google Drive API v2,因此请确保您使用v2 搜索参数页面,
- ID 是 Google Drive 分配的文件夹 ID,这个 SO 问题列出了查找 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']))