Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access my Google Drive files from Google Colab?

I downloaded images from a url using urlretrieve (urllib) in Google Colab. However, after downloading the images, I am not able to locate the images.

like image 636
Hashim Abu Sharkh Avatar asked Aug 08 '19 19:08

Hashim Abu Sharkh


People also ask

How do I open the drive folder in Colab?

To import google drive, write this code in code section of colab and run it by Ctrl+Enter . On running code, one blue link and a text box will appear we need to provide a permission text. So click the link and a new tab will open where you will be asked for permission to access google drive.


1 Answers

from google.colab import drive
drive.mount('/content/gdrive', force_remount=True)

root_dir = "/content/gdrive/My Drive/"
base_dir = root_dir + 'my-images/'

Now you may access your Google Drive as a file system using standard python commands to read and write files. Don’t forget to append base_dir before root path(s) where you need to use. Here the base_dir variable expects a folder named my-images at the location pointed by root_dir.

enter image description here enter image description here

Google Colab lets you access and use your Google Drive as a storage for a time constrained runtime session. It is not a disk storage on your local machine or physical computer. Notebooks run by connecting to virtual machines that have maximum lifetimes that can be as much as 12 hours.

Notebooks will also disconnect from VMs when left idle for too long which in turn also disconnects your Google Drive session. The code in the answer only shows one of many ways to mount your Google Drive in your colab runtime's virtual machine.

For more documentation, refer to this link and faq.

like image 170
sparsh Avatar answered Oct 16 '22 11:10

sparsh