pythonpython-3.xgoogle-apigoogle-drive-apipydrive

How to set folder path when downloading from google drive


I am trying to download some files from a google drive folder to local folder /home/lungsang/Desktop/gdrive/ABC. Can you guys can modify the below code so that I can achieve it? PS: Right now its just downloading in the root folder :)

import streamlit as st
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)


folder = '1tuQxaiDOdbfv1JHXNAln2nbq1IvBOrmP'

file_list = drive.ListFile({'q': f"'{folder}' in parents and trashed=false"}).GetList()
for index, file in enumerate(file_list):
    print(index+1, 'file Downloaded : ', file['title'])
    file.GetContentFile(file['title'])


Solution

  • In your script, how about the following modification? Please add the path as follows.

    Modified script:

    import streamlit as st
    from pydrive.auth import GoogleAuth
    from pydrive.drive import GoogleDrive
    
    gauth = GoogleAuth()
    gauth.LocalWebserverAuth()
    drive = GoogleDrive(gauth)
    
    
    path = "/home/lungsang/Desktop/gdrive/ABC/" # Added
    
    folder = '1tuQxaiDOdbfv1JHXNAln2nbq1IvBOrmP'
    file_list = drive.ListFile({'q': f"'{folder}' in parents and trashed=false"}).GetList()
    for index, file in enumerate(file_list):
        print(index+1, 'file Downloaded : ', file['title'])
        file.GetContentFile(path + file["title"]) # Modified