Skip to content

Instantly share code, notes, and snippets.

@korakot
Last active January 31, 2023 15:33
Show Gist options
  • Save korakot/51a917e1f53891d53be223439b0f75c1 to your computer and use it in GitHub Desktop.
Save korakot/51a917e1f53891d53be223439b0f75c1 to your computer and use it in GitHub Desktop.
List all files under a publicly-shared folder in Google Drive (in Colab), and download them
from google.colab import auth
auth.authenticate_user() # must authenticate
'''list all ids of files directly under folder folder_id'''
def folder_list(folder_id):
from googleapiclient.discovery import build
gdrive = build('drive', 'v3').files()
res = gdrive.list(q="'%s' in parents" % folder_id).execute()
return [f['id'] for f in res['files']]
'''download all files from a gdrive folder to current directory'''
def folder_download(folder_id):
for fid in folder_list(folder_id):
!gdown -q --id $fid
'''This one download the folder recursively'''
def folder_download(folder_id):
# authenticate
from google.colab import auth
auth.authenticate_user()
# get folder_name
from googleapiclient.discovery import build
service = build('drive', 'v3')
folder_name = service.files().get(fileId=folder_id).execute()['name']
# import library and download
!wget -qnc https://github.com/segnolin/google-drive-folder-downloader/raw/master/download.py
from download import download_folder
download_folder(service, folder_id, './', folder_name)
return folder_name
@rewiaca
Copy link

rewiaca commented Feb 26, 2020

What is !gdown -q --id $fid ?

@korakot
Copy link
Author

korakot commented Feb 27, 2020

It's an equivalent of

import gdown
url = 'https://drive.google.com/uc?id=' + fid
gdown.download(url, None, True)

@Alamnoor
Copy link

Alamnoor commented May 3, 2020

how to use this function? Can you please explain step by step?
I am new with colab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment