Google offers the Google Colab service to numerous researchers and developers worldwide. It is an environment similar to a Jupyter Notebook in a single location without any prerequisites. It is free to use with a restricted set of computer resources and engines, including free access to GPUs (Graphics Execution Units) for accelerated parallel execution of code. It also has a premium edition with more readily available computing resources. It is one of the best resources for any students interested in computer science, particularly machine learning, data science, artificial intelligence, etc. Python3 or R notebooks are available as a free workspace from Colab. These notebooks make it simple to implement many compute-intensive activities.
This article will teach us to unmount Google Drive from our Google Colab notebook. You can also refer to the video solution for this end which is attached at the end of this article.
Prerequisite for the task – A Google Account. Refer to this article which explains how to create a Gmail account, which is equivalent to creating a Google Account.
What is mounting the drive on Colab?
It is a process to access the files and resources from drive storage directly into the Colab notebook. This process helps in managing persistent storage for the Colab notebook as the files still persist in storage even if the Colab notebook gets disconnected from its computational resources.
What is unmounting the drive on Colab?
It is a process opposite to mounting. In remounting, the existing access to drive files and resources from the Colab notebook is removed. It is done when the files are no longer required or they are stored on some other drive. So, it is to disconnect from one drive and connect to the other one.
Mount Google Drive in Google Colab
This is one of the most common ways to get the required files into our Colab session storage. This way, we need not upload our files every time onto the colab, rather we would just upload it only once on Google Drive and then mount the drive storage into our Colab notebook any number of times using the following lines of code. In order to mount our drive, we need to give permission to Google Colab to access the drive storage.
Step 1: Use the following piece of code in order to connect to your google drive using colab notebook. Using this code, the google colab notebook can be connected to google drive in order to access the files and resources on drive directly into colab notebook.
from google.colab import drive
drive.mount('/content/drive')
%cd path/to/your/folder
%ls
By following the above steps, we can easily upload folders to the colab notebook.
Unmount Google Drive in Google Colab
The already mounted drive on google colab can be easily unmounted by running the following command. This is usually does when the access to a particular drive files and resources is not required anymore. This can also be done when the user wants to connect to some other google drive account. Hence, the process is to unmount the previous one and remount the new drive account.
from google.colab import drive
drive.flush_and_unmount()
“Flush_and_umount()” command will unmount the the already mounted drive and then using the command given in the previous section of the article, we can easily remount another drive onto our colab notebook after disconnecting the session and reconnecting it.
Refer to the following video, to get a more detailed idea about the process which showcase all the steps mentioned above.