【问题标题】:Is there a way to get title of a google drive file in python using its google drive file id?有没有办法使用其谷歌驱动器文件 ID 在 python 中获取谷歌驱动器文件的标题?
【发布时间】:2023-01-20 22:07:11
【问题描述】:

我有许多要下载的文件的谷歌驱动器 ID。 但是,下载谷歌驱动器文件的 api 也需要文件名,以便用该名称保存文件。

python 中有没有办法从 python 中的谷歌驱动器文件 ID 获取文件的标题/名称?

如果是这样,请帮助分享示例代码。

【问题讨论】:

  • 请编辑您的问题并包括您的代码下载不需要文件名只需要 id。

标签: python-3.x google-drive-api


【解决方案1】:

file.get 下载文件的方法不需要文件名,它只需要您向它发送文件 ID。

# Call the Drive v3 API
# get the file media data
request = service.files().get_media(fileId=FILEID)

fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
    status, done = downloader.next_chunk()
    print("Download %d%%" % int(status.progress() * 100))

需要名称的是当您要将其保存到系统中时。

# The file has been downloaded into RAM, now save it in a file
fh.seek(0)
with open(file_name, 'wb') as f:
    shutil.copyfileobj(fh, f, length=131072)

您可以先执行 file.get 以获取文件的元数据,然后在您想要保存文件时使用它。

# Call the Drive v3 API
# Get file name, so we can save it as the same with the same name.
file = service.files().get(fileId=FILEID).execute()
file_name = file.get("name")
print(f'File name is: {file_name}')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多