2

I am trying to download a file from google drive with file ID and then place the file in my local folder. Download works fine but my problem is that when I try to specify the folder and file name, I can specify the filename manually, but not automated. I mean I want the filename which is fetched from googledrive should go automatically in my specified folder without any manual input as filename. Here is my code:

#!/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')
Hossain
  • 235
  • 1
  • 3
  • 15

1 Answers1

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

This might help you!

cvakiitho
  • 1,377
  • 13
  • 27