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.
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())
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.
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'
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()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.