How to change the working directory in a Jupyter Notebook?

I could not find a place for me to change the working directory in the Jupyter Notebook, so I could not use the pd.read_csv method to read in a specific csv document.

Is there any way to do this? FYI, I am currently using Python3.5.1.

Thanks!

+22
source share
6 answers

Running os.chdir(NEW_PATH) will change the working directory.

 import os os.getcwd() Out[2]: '/tmp' In [3]: os.chdir('/') In [4]: os.getcwd() Out[4]: '/' In [ ]: 
+32
source

First you need to create a configuration file using cmd : jupyter notebook --generate-config Then find the folder C: \ Users \ your_username \ .jupyter ( Search for this folder ) and right-click on the jupyter_notebook_config.py edit.

Then press Ctrl + F: # c.NotebookApp.notebook_dir = '' . Note that quotation marks are single. Select the directory that you want to use as your home for your jupyter, and copy it with Ctrl + C , for example: C: \ Users \ username \ Python Projects.

Then paste it into this line as follows: c.NotebookApp.notebook_dir = 'C: \\ Users \\ username \\ Python Projects'

Be sure to delete # , as in the comment.

Make sure to double the slash \\ on each name of your path. Ctrl + S to save config.py !!!

Go back to your cmd and run jupyter notebook. This should be in your directory of choice. Check this by creating a folder and watching your directory from your computer.

+9
source

You can use jupyter magic as below

 %cd "C:\abc\xyz\" 
+8
source

this is similar to Jason Lee, as he mentioned earlier:

in Jupyter notepad, you can access the current working directory :

 pwd() 

or by importing the OS from the library and running os.getcwd()

i.e. for example

 In[ ]: import os os.getcwd( ) out[ ]: :c\\users\\admin\\Desktop\\python (#This is my working directory) 

Change working directory

To change the working directory (much more similar to the current Wd, you just need to switch from os.getcwd() to os.chdir('desired location')

 In[ ]: import os os.chdir('c:user/chethan/Desktop') (#This is where i want to update my wd, like that choose your desired location) out[ ]: 'c:user\\chethan\\Desktop' 
+3
source

Jupyter in WinPython has a batch file in the scripts folder called:

 make_working_directory_be_not_winpython.bat 

You need to edit the following line:

 echo WINPYWORKDIR = %%HOMEDRIVE%%%%HOMEPATH%%\Documents\WinPython%%WINPYVER%%\Notebooks>>"%winpython_ini%" 

replacing the Documents\WinPython%%WINPYVER%%\Notebooks address of your folder.

Please note that the %%HOMEDRIVE%%%%HOMEPATH%%\ identifies the root and user folders (i.e. C:\Users\your_name\ ), which allows you to specify different WinPython settings on separate computers in the same cloud storage folder (for example, OneDrive), access and work with the same files from different machines. I find it very helpful.

+1
source

on a jupyter laptop try this:

 pwd #this shows the current directory 

if this is not the directory you like and you want to change it, try this:

 import os os.chdir ('THIS SHOULD BE YOUR DESIRED DIRECTORY') 

Then try pwd again to see if the directory is right for you.

It works for me.

+1
source

All Articles