Tanmay Bhatnagar
Tanmay Bhatnagar

Reputation: 2470

How to determine file path in Google colab?

I mounted my drive using this : from google.colab import drive drive.mount('/content/drive/')

I have a file inside a folder that I want the path of how do I determine the path? Say the folder that contains the file is named 'x' inside my drive

Upvotes: 10

Views: 53894

Answers (5)

AmBha
AmBha

Reputation: 11

Seems like Colab has experienced a decent amount of updates since this question was asked. First, mount your gdrive with

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

On the left, you should see a folder icon. Click that and navigate to the appropriate file in your gdrive. Once you find your file, right click on it and chose copy path. This path is the path to your file so your Google Colab can find it.

Upvotes: 1

Adnan Ali
Adnan Ali

Reputation: 3055

This answer maybe useful for anyone as I was facing the problem and came to this question.

My file utils.py is located in "MyDrive/NotebookDatasets/CMVRLG" and I was trying to import it.

This is how we can import it. To mount google drive:

import os
from google.colab import drive
drive.mount('/content/gdrive')
import sys
sys.path.append('drive/gdrive/MyDrive/NotebookDatasets/CMVRLG')

and here is how we can import

import gdrive.MyDrive.NotebookDatasets.CMVRLG.utils

Upvotes: 0

stevec
stevec

Reputation: 52218

I can't find any easy way to do this except type it out manually.

For me, it looked like this

dat = ZipFile('/content/drive/My Drive/Code/Tutorial/Assign 2 - New/weblogs.zip', 'r')

Note that:

  • You must respect spaces.
  • You do not need to escape spaces - i.e. "My Dir" should simply be 'My Dir', not 'My\ Dir'.

Upvotes: 0

MihneaAM
MihneaAM

Reputation: 71

The path as parameter for a function will be /content/drive/My Drive/x/the_file, so without backslash inside My Drive

Upvotes: 7

Bob Smith
Bob Smith

Reputation: 38579

The path will be /content/drive/My\ Drive/x/the_file.

Upvotes: 13

Related Questions