How to Specify a Folder Path in Google Colab: A Tutorial

How to Specify a Folder Path in Google Colab: A Tutorial


How to use Google Colab for Python (power tool to analyze data)?


Last time, I introduced how to mount your Google Drive in Google Colab. This time, I will explain how to set a folder path in Google Colab. First, let’s establish the connection between Google Colab and your Google Drive. The code is as follows:

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

Then, the folders from your Google Drive will appear in the left sidebar of Google Colab.

I created a folder called “coding_practice” in MyDrive folder and uploaded an Excel file named “experiment” into it. I would like to perform various data analysis using Python code. Therefore, I want to upload this Excel file to Google Colab.

First, let’s import pandas.

import pandas as pd

and then, let’s upload the Excel file. However, the following error occurs.

It says that it cannot find the “experiment” data. The reason is that the folder path where the file is located has not been specified. Therefore, you need to set the folder path.



Right-click on the folder where the file is located and click on “Copy path” from the menu.

Then, the file path will be copied. In this case, the file path is /content/drive/MyDrive/coding_practice. Now, let’s set this file path to the folder where the file is located.

Enter the following code:

%cd /content/drive/MyDrive/coding_practice

Now, let’s try uploading the Excel file again.

You can see that the Excel file has been successfully uploaded.



You can also set the file path using the following code:

import os
os.chdir('/content/drive/MyDrive/coding_practice')


Leave a Reply

If you include a website address in the comment section, I cannot see your comment as it will be automatically deleted and will not be posted. Please refrain from including website addresses.