Python Forum
Navigating file directories and paths inside Jupyter Notebook
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Navigating file directories and paths inside Jupyter Notebook
#1
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 .
Reply
#2
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).
Mark17 likes this post
Reply
#3
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).
Reply
#4
When cd in cell it work as it dos in command line.
Also can have no comments or other stuff,or will get SyntaxError.
Reply
#5
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.
Mark17 likes this post
Reply
#6
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm trying to import a dataset into a Jupyter Notebook Hisilat03 0 749 Mar-24-2025, 09:26 AM
Last Post: Hisilat03
  Jupyter Notebook cell ? flaviu2 6 5,023 Feb-10-2025, 09:39 AM
Last Post: mysqlguider
  png-file in Jupyter bunnkvarken 0 863 Oct-22-2024, 02:47 PM
Last Post: bunnkvarken
  import data (.csv) into Jupyter notebook oranstprotonme 2 1,248 Aug-14-2024, 07:08 PM
Last Post: oranstprotonme
  rename same file names in different directories elnk 5 2,471 Jul-12-2024, 01:43 PM
Last Post: snippsat
  Troubleshooting Jupyter Notebook installation with Python using pip and a venv Drone4four 1 2,288 Jun-04-2024, 10:55 PM
Last Post: Drone4four
  Organization of project directories wotoko 3 1,515 Mar-02-2024, 03:34 PM
Last Post: Larz60+
  Absolute paths in subprocess - file not found kittyticker 4 3,057 Jan-28-2024, 10:37 PM
Last Post: kittyticker
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 4,382 Nov-15-2023, 06:56 PM
Last Post: jst
  Listing directories (as a text file) kiwi99 1 1,415 Feb-17-2023, 12:58 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020