Find path of python_notebook.ipynb when running it with Google Colab
Asked Answered
R

3

9

I want to find the cwd where my CODE file is stored.

With jupiter Lab i would do:

import os 
cwd= os.getcwd()
print (cwd)
OUT: 
'C:...\\Jupiter_lab_notebooks\\CODE'

However,if i copy the folders to my GoogleDrive, and run the notebook in GOOGLE COLAB, i get:

import os 
cwd= os.getcwd()
print (cwd)
OUT: 
/content

No matter where my notebook is stored. How do i find the actual path my .ipynb folder is stored in?

#EDIT

What i am looking for is python code that will return the location of the COLAB notebook no matter where in drive it is stored. This way i can navigate to sub-folders from there.

Revalue answered 12/2, 2021 at 16:24 Comment(0)
M
4

This problem has been bothering me for a while, this code should set the working directory if the notebook has been singularly found, limited to Colab system and mounted drives, this can be run on Colab:

import requests
import urllib.parse
import google.colab
import os

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


def locate_nb(set_singular=True):
    found_files = []
    paths = ['/']
    nb_address = 'http://172.28.0.2:9000/api/sessions'
    response = requests.get(nb_address).json()
    name = urllib.parse.unquote(response[0]['name'])

    dir_candidates = []

    for path in paths:
        for dirpath, subdirs, files in os.walk(path):
            for file in files:
                if file == name:
                    found_files.append(os.path.join(dirpath, file))

    found_files = list(set(found_files))

    if len(found_files) == 1:
        nb_dir = os.path.dirname(found_files[0])
        dir_candidates.append(nb_dir)
        if set_singular:
            print('Singular location found, setting directory:')
            os.chdir(dir_candidates[0])
    elif not found_files:
        print('Notebook file name not found.')
    elif len(found_files) > 1:
        print('Multiple matches found, returning list of possible locations.')
        dir_candidates = [os.path.dirname(f) for f in found_files]

    return dir_candidates

locate_nb()
print(os.getcwd())
Mackenziemackerel answered 11/3, 2022 at 11:46 Comment(2)
ok so it is walking though every path until it matches the filename, thanksRevalue
I wish there was a better way to do soDyestuff
P
2

Google colab allows you to save notebooks to google drive and when you create a new notebook, you can click "Locate in drive" in the file menu to access this location.

enter image description here

Please note, that you can mount your drive to a colab instance which means you can access the google drive as a sub-folder in the colab instance, however, if you are looking for the physical location in your local google drive folder (created by google drive app) then this should be something like this location (on Mac).

/Volumes/GoogleDrive/My Drive/Colab Notebooks/

If however, you have mounted your google drive on your colab, and are looking for where the notebooks are saved, via google colab, try this -

path = '/content/drive/MyDrive/Colab Notebooks'
Prober answered 9/10, 2021 at 18:7 Comment(1)
Sorry i wasn't clear with my question: What i am looking for is python code that will return the location of the COLAB notebook no matter where in drive it is stored. This way i can navigate to sub-folders from there. (eg a data folder )Revalue
W
0

Since GoogleDrive is your current working directory, you need to mount it from colab first:

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

then you need to change your directory to where your .ipynb stored:

import os 
os.chdir('/content/drive/MyDrive/path/to/your/folder')

Finally, if you do

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

you will get something like /content/drive/MyDrive/path/to/your/folder where your .ipynb is stored.

And when you do

import subprocess
subprocess.check_output(['ls'])

you will see your notebook file listed there.

If you want to navigate to sub-folders, just use os.chdir().

Woodcock answered 15/10, 2021 at 8:27 Comment(3)
So there is no way of finding the path : '/content/drive/MyDrive/path/to/your/folder'? a command like "get path of current notebook".Revalue
I think that is os.getcwd() actually doing, right?Woodcock
Yes, however when you mount your drive you and run os.getcwd() you get '/content/' no matter what folder your notebook started in your google drive. I want to RUN a nobetook and find out in what subfolder of google drive it is stored - this way i can then navigate relative to that fodler, so that if I move the set of folders where the notebook is, everything still worksRevalue

© 2022 - 2024 — McMap. All rights reserved.