M.Gharabeel
M.Gharabeel

Reputation: 43

How to download files to Google Drive using Google colab?

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)

Upvotes: 4

Views: 14252

Answers (2)

Abhijeet Konduskar
Abhijeet Konduskar

Reputation: 111

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.

Upvotes: 11

Barak Itkin
Barak Itkin

Reputation: 4877

Don't download the file into Google Drive, as there's overhead to access Google Drive in Colab, and especially to write files there.

If this file is temporary, just download it to /tmp (or use tempfile.gettempdir to make the code prettier). If it's not temporary, still consider downloading it to a temporary folder and then copying it at the end of the download to Drive (while continuing to work with the local copy for speed).

Upvotes: 0

Related Questions