lungsang
lungsang

Reputation: 167

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'])

Upvotes: 1

Views: 584

Answers (1)

Tanaike
Tanaike

Reputation: 201378

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
  • In this modification, it supposes that the directory of /home/lungsang/Desktop/gdrive/ABC/ is existing. Please be careful about this.

Upvotes: 1

Related Questions