Hashim Abu Sharkh
Hashim Abu Sharkh

Reputation: 83

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.

Upvotes: 7

Views: 17098

Answers (2)

Savas Yıldırım
Savas Yıldırım

Reputation: 21

Suppose, your working Google Drive Folder is

Colab Notebooks/STUDY/

Its actual path is drive/My Drive/Colab Notebooks/STUDY

1) First mount and authenticate yourself with the following code

from google.colab import drive
drive.mount('/content/drive')

2) Second change your current folder to point working folder STUDY

os.chdir("drive/My Drive/Colab Notebooks/STUDY")
os.listdir()

Done!

Upvotes: 2

Sparsh
Sparsh

Reputation: 1488

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.

Upvotes: 11

Related Questions