Python – How to download files to Google Drive using Google colab

downloadfilegoogle-colaboratorypython

I've been using this code to mount Colab with Google Drive and download any file by pasting in download URL but I have noticed that it take quite long even though the files are few megabytes in size. Is there anything that can be done to improve it.

**First cell:**
from google.colab import drive
drive.mount('/content/gdrive')
root_path = 'gdrive/My Drive/' 

**Second cell:**
import requests  
file_url = "DOWNLOAD URL HERE"

r = requests.get(file_url, stream = True)  

with open("/content/gdrive/My Drive/FILE NAME HERE", "wb") as file:  
    for block in r.iter_content(chunk_size = 1024): 
         if block:  
             file.write(block)

Best Answer

I prefer using !wget method.

!wget "Specify URL you want to download" -P "specify DIRECTORY where you want contents of URL"

For example.

!wget "https://github.com/jbrownlee/Datasets/releases/download/Flickr8k/Flickr8k_Dataset.zip" -P "/content/drive/My Drive/imgcaptiongen/data"

It's much easier this way.

Related Topic