Python google drive文件下载无需指定目标文件名

问题描述 投票:1回答:1

我试图从google drive下载一个文件,文件ID,然后把文件放在我的本地文件夹中。下载工作正常,但我的问题是,当我试图指定文件夹和文件名,我可以手动指定文件名,但不能自动。我的意思是,我希望从googledrive获取的文件名应该自动进入我指定的文件夹,而不需要任何手动输入作为文件名。这是我的代码。

#!/usr/bin/python3
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
# Try to load saved client credentials
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
# Authenticate if they're not there
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
    # Refresh them if expired
    gauth.Refresh()
else:
    # Initialize the saved creds
    gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("mycreds.txt")
drive = GoogleDrive(gauth)

file1 = drive.CreateFile({'id':'xxx'})
file1.GetContentFile('/home/smc/sw_files/xxx.zip')
python-3.x google-drive-api pydrive
1个回答
0
投票
def filename(id):
    url = 'https://drive.google.com/uc?export=download&id='+id
    response =  requests.get(url)
    header = response.headers['Content-Disposition']
    file_name = re.search(r'filename="(.*)"', header).group(1)
    print("Gotcha ! File Name is --> "+file_name)
    return file_name

这可能会帮助你!

© www.soinside.com 2019 - 2024. All rights reserved.