ML1
ML1

Reputation: 119

How to obtain the current path to my google colab file?

I work with google colab on my google drive. When I do

import os 
cwd= os.getcwd()
print (cwd)

it return

/content

. How to obtain the path to my google colab file like

/content/drive/path_to_my_current_google_colab_file

Upvotes: 4

Views: 4666

Answers (2)

The other answer relies on there is no other file in the file system having the same name, but it is not always true...

To have a complete solution, we could only relies a new native function provided by Colab, but we don't have one so far. Colab users have opened two issues to suggest Google to fix this bug. https://github.com/googlecolab/colabtools/issues/1570 https://github.com/googlecolab/colabtools/issues/4069

For those who wants a better solution, please go provide more feedback to Colab teams...

Upvotes: 1

Victor Novak
Victor Novak

Reputation: 67

from google.colab \
import drive
import os, glob

my_name = 'my.ipynb'

drive.mount(os.getcwd() + '/drive')
my_path = glob.glob(os.getcwd() + '/**/' +
my_name, recursive = True)

# some 'list comprehension'.
my_path = [my_i
for my_i in my_path if len(my_i) in [max([len(my_i)
for my_i in my_path])]
][0]; print(my_path)

Upvotes: 1

Related Questions