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
  Organization of project directories wotoko 3 439 Mar-02-2024, 03:34 PM
Last Post: Larz60+
  Absolute paths in subprocess - file not found kittyticker 4 500 Jan-28-2024, 10:37 PM
Last Post: kittyticker
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 1,052 Nov-15-2023, 06:56 PM
Last Post: jst
  Listing directories (as a text file) kiwi99 1 846 Feb-17-2023, 12:58 PM
Last Post: Larz60+
  How to programmatically stop a program in Jupyter Notebook? Mark17 11 37,249 Feb-12-2023, 01:41 PM
Last Post: jp21in
Thumbs Up Python 3 Jupyter notebook ternary plot data nicholas 0 953 Jan-21-2023, 05:01 PM
Last Post: nicholas
  Find duplicate files in multiple directories Pavel_47 9 3,144 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  Graphs from Jupyter notebook to word Scott 0 891 Nov-28-2022, 06:16 AM
Last Post: Scott
  rename same file names in different directories elnk 0 719 Nov-04-2022, 05:23 PM
Last Post: elnk
  Read xml column inside csv file with Python estertabita 2 1,366 Jul-26-2022, 06:09 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