Python Forum
Navigating file directories and paths inside Jupyter Notebook - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Navigating file directories and paths inside Jupyter Notebook (/thread-41007.html)



Navigating file directories and paths inside Jupyter Notebook - Mark17 - Oct-28-2023

Hi all,

I want to become fluent with entering code in JN to successfully navigate my directories.

For example, I want to import a .csv file like this:

import pandas as pd
cereal = pd.read_csv('cereal.csv', index_col = 0)
I get:

FileNotFoundError: [Errno 2] No such file or directory: 'cereal.csv'

What's the most straightforward way to determine what directly I'm currently in and how to get to the directory I want to go? It's not all that complicated on my computer: Desktop > Desktop Transfer Material > Miscellaneous > Python .


RE: Navigating file directories and paths inside Jupyter Notebook - snippsat - Oct-28-2023

To get directory you are in eg pwd or !pwd or echo %cd%(if don't have pwd) in cell.
Can use Python to,in cell do.
import os

os.getcwd()
If eg i have 'cereal.csv' in a folder i can cd directly in cell.
cd G:/div_code/ur_env
List files from cell ls or !dir

When start Notebook can start from directory where have files that want to work with.
jupyter lab --notebook-dir="G:/div_code"
Or i just start jupyter lab from directory i want to work from.
A lot stuff work,see also that can use command line stuff directly(add ! then most works) in cell like eg cd !dir or ls(if have on Windows as i have).


RE: Navigating file directories and paths inside Jupyter Notebook - Mark17 - Oct-28-2023

That helps.

Why does this work...

cd Desktop\Desktop Transfer Material\Miscellaneous\Python
...whereas this...

cd Desktop\Desktop Transfer Material\Miscellaneous\Python #no backslash before first folder to navigate into
#To go up a level, type cd ..
gives an error:

File "<ipython-input-46-1f2f99e3e15f>", line 1
cd Desktop\Desktop Transfer Material\Miscellaneous\Python #no backslash before first folder to navigate into
^
SyntaxError: invalid syntax

I'm typing into a JN cell so I expect the # and everything following on L1 and L2 to be ignored (commented out).


RE: Navigating file directories and paths inside Jupyter Notebook - snippsat - Oct-28-2023

When cd in cell it work as it dos in command line.
Also can have no comments or other stuff,or will get SyntaxError.


RE: Navigating file directories and paths inside Jupyter Notebook - deanhystad - Oct-28-2023

This is not python
cd Desktop\Desktop Transfer Material\Miscellaneous\Python
cd is a "magic command". When notebook sees "cd " at the start of the line it calls a function to change the current directory.


RE: Navigating file directories and paths inside Jupyter Notebook - Mark17 - Oct-29-2023

(Oct-28-2023, 09:01 PM)snippsat Wrote: When cd in cell it work as it dos in command line.
Also can have no comments or other stuff,or will get SyntaxError.

Got it. Thanks!