Last active
January 31, 2023 15:33
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''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 |
What is !gdown -q --id $fid
?
It's an equivalent of
import gdown
url = 'https://drive.google.com/uc?id=' + fid
gdown.download(url, None, True)
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
Can test with folder_id="1HvIeNhqtFVllXFWzH5NawDdnIfgGDwCK"