75

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

Is there any way to make it? FYI, I'm using Python3.5.1 currently.

Thanks!

Y. Gao
  • 959
  • 1
  • 7
  • 7
  • 7
    Aside: You don't have to change directory to read a file that resides in a non-current directory. You could `pd.read_csv('/home/ygao/homework/table.csv')` or `pd.read_csv('c:/users/ygao/documents/table.csv')`, for example. – Robᵩ Feb 27 '16 at 03:21

11 Answers11

90

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 [ ]:
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Thanks for your answer and I succeeded changing the working directory. Here's another issue during my practice: when I wanted to change back the working directory, which is: C:\Users\Yuan Gao, notebook prompts the following: 'SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape'. However, if I coded as `os.chdir('C:\\Users\Yuan Gao')`, then there's no problem. Why is that? – Y. Gao Feb 27 '16 at 14:35
  • 4
    That occurs because the backslash character, \, is an overused workhorse that means different things according to the context. In particular, it is both the directory separator in Windows and the escape character in python strings. For me, the simplest solution is to always use forward slash, /, in paths: `os.chdir('C:/Users/Yuan Gao')` – Robᵩ Feb 27 '16 at 23:04
44

You may use jupyter magic command as below

%cd "C:\abc\xyz\"
0x26res
  • 11,925
  • 11
  • 54
  • 108
ManojK
  • 441
  • 4
  • 4
27

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

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

Then on that line, paste it like this : c.NotebookApp.notebook_dir = 'C:\\Users\\username\\Python Projects'

Make sure to remove #, as it is as comment.

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

Go back to your cmd and run jupyter notebook. It should be in your directory of choice. Test it by making a folder and watch your directory from your computer.

Community
  • 1
  • 1
  • That helped. But the flag -config is not necessary to generate the configuration file. – Vijay Nov 23 '17 at 06:02
  • 2
    The flag generate config file is now `--generate-config` – Shayan RC Jan 06 '18 at 10:13
  • 1
    The os.chdir('path_name') works only during that session of Jupyter Notebook that you're currently working on, so, if you will try to work on some files that are in the desired path on a different Jupyter Notebook session, you will again come across the error of not being able to find the file. A permanent solution is what @George Petropoulos has told here. Go with the method that suits you the most. – Jyotsna Masand Jan 09 '21 at 05:56
14

on Jupyter notebook, try this:

pwd                  #this shows the current directory 

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

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

Then try pwd again to see if the directory is what you want.

It works for me.

Jason Lee
  • 141
  • 1
  • 2
5

it is similar to jason lee as he mentioned earlier:

in Jupyter notebook, you can access the current working directory by

pwd()

or by import OS from 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)

Changing Working Directory

For changing the Working Directory (much more similar to current W.d just you need to change 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 w.d, 
                                                  like that choose your desired location)
out[  ]: 'c:user\\chethan\\Desktop'
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
chethan
  • 59
  • 1
  • 1
4

It's simple, every time you open Jupyter Notebook and you are in your current work directory, open the Terminal in the near top right corner position where create new Python file in. The terminal in Jupyter will appear in the new tab. Type command cd <your new work directory> and enter, and then type Jupyter Notebook in that terminal, a new Jupyter Notebook will appear in the new tab with your new work directory.

Mawanda
  • 61
  • 6
2

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

make_working_directory_be_not_winpython.bat

You need to edit the following line in it:

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

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

Notice that the %%HOMEDRIVE%%%%HOMEPATH%%\ part will identify the root and user folders (i.e. C:\Users\your_name\) which will allow you to point different WinPython installations on separate computers to the same cloud storage folder (e.g. OneDrive), accessing and working with the same files from different machines. I find that very useful.

1

I have did it on windows machine. Detail mentioned below

  1. From windows start menu open “Anaconda Prompt 1

  2. Find .jupyter folder file path . In command prompt just type 2 or 3 to find the .jupyter path

  3. After find the .jupyter folder, check there has “jupyter_notebook_config” file or not. If it is not there then run below command 4

After run the command it will create "jupyter_notebook_config.py"

if do not have administrator permission then Some time you could not find .jupyter folder . Still you can open config file from any of the text editor

  1. Open “jupyter_notebook_config.py” file from the the “.jypyter” folder.
  2. After open the file need to update the directory is use for notebooks and kernel. There are so many line in config file so find “#c.NotebookApp.notebook_dir” and update the path 5

After:

6

Save the file 6. Now try to create or read some file from the location you set

eshirvana
  • 23,227
  • 3
  • 22
  • 38
Wow
  • 49
  • 3
1

Open jupyter notebook click upper right corner new and select terminal then type cd + your desired working path and press enter this will change your dir. It worked for me

Jeff Linahan
  • 3,775
  • 5
  • 37
  • 56
0
  1. list all magic command %lsmagic
  2. show current directory %pwd
tharsikan
  • 41
  • 5
  • 1
    Let's say I am with stupid and want to type your command somewhere... Where would that "somewhere" be ... In the GUI console of ERP software or somewhere else? If updated you might get upvotes too... and thus XP ;-) – ZF007 Aug 14 '20 at 10:55
  • You want to type it in a cell of a jupyter notebook. – Mac C. Nov 18 '20 at 14:51
0

What works for me on Windows is creating a shortcut to jupyterlab, and altering the properties of the shortcut.

At "Start in:", enter your desired working directory. Using this method, you don't have to configure a global file either.