1

我正在尝试从带有文件 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')
4

1 回答 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

这可能对你有帮助!

于 2020-04-22T11:15:16.900 回答