How To Change The Working Directory In A jupyter Notebook in 2 simple methods?

More often than not when we are coding, we encounter situations when all the code we wanted to write happens to end up in a destination other than the one we intended to put it in. This simple mistake can completely change your analysis or take your hard work to vain. This is where it becomes imperative to change the working directory as and how required by the code. Let’s dive right into it, for this quick method, we’ll use Python 3 to change the working directory in a Jupyter Notebook with not more than 5 lines of code. ( and an additional method to change the default working directory )

A short note to anyone new to Python:

Python is a programming language that one does not need to compile. You can just run it line by line (which is how we can use it in a notebook). So if you are quite new to programming, Python is a great place to start. The current version is Python 3. Python is relatively the most sought-after programming language of this decade and considering the boom of Data Science in 2017, there’s no doubt that the demand for skills in Python will only increase in the future.

In the last decade, writing code was really difficult considering the difficulty of syntaxes and the knowledge and skill required to execute a computer language. However, this is not the case today when Python has been one of the easiest languages to learn yet one of the most powerful ones. It’s simple English and with a little bit of grammar and logic, one can execute machine learning code without having to have a computer science major in their college.

In this article, we’ll go over two methods to change the working directory as well as the Default working directory while working with a Jupyter Notebook. The first method focuses on changing the working directory using the OS library which needs not more than 4 lines of code and the second method focuses on changing the default working directory to avoid the hassle of doing it repeatedly using a simple config file.

Also Read -> Jupyter vs Colab which one is better in 2022

What is a Jupyter Notebook?

One can use an IDE or integrated development environment to code in Python but there is no question that the easier way to code in Python is to use a Jupyter notebook. It can combine programming, text, and images in one platform that an individual can run on their local machine. In a notebook, everything is laid out in cells. Text cells and code cells are the most common.

To know more about how to use a Jupyter notebook, refer to this simple tutorial provided by the Google Colaboratory which is closely related to how a Jupyter notebook works: How to work on a notebook (Google Colab Edition) <- click here

When one installs the Anaconda Navigator or accesses a Jupyter notebook installed otherwise on their local system, it becomes a hassle to save files in different places as the files are saved by default in the location where the Anaconda Navigator’s environment is installed. In this article, we’ll go over a simple method that will help you change your working directory in a minute.

How to change the working directory in a Jupyter Notebook?

Step 1: Check your current working directory

pwd

pwd (present working directory) instructs Jupyter Notebooks to return the directory where the notebook is currently functioning. If this is the directory you want to continue with, anything you do or any files you save can impact the location specified, follow through with the next steps to change the working directory.

Step 2: Import OS Library to the Jupyter Notebook

To make this process easier, we’ll take the help of the ‘OS’ Library of Python i.e. Operating Systems. You can find out your current working directory using commands from OS too after you have successfully imported the library

import os
os.getcwd( )

Step 3: Copy the desired file path that you want to change the working directory to

If you want to change the directory, go to the folder you want to the change the directory to, and using properties, copy the file path which looks like this: C:\Users\Username\OneDrive\Desktop\

Once you have copied the desired file path, insert it in the following code as specified

Step 4: Use the ‘chdir’ command of the OS Library to change the working directory in a Jupyter Notebook

os.chdir('desired filepath here') 

The OS command – ‘chdir’ i.e. Change Working Directory will help you change the working directory in the Jupyter notebook and now you can work in the desired directory. To confirm where the jupyter notebook is functioning, use the os.getcwd() or pwd commands.

Change the working directory in a Jupyter notebook in less than 5 lines of code
Changing Working Directory in a Jupyter Notebook in less than 5 lines of code!

How to Change the default working directory in a Jupyter Notebook?

While changing the working directory every time can be quite a task if you wish to pursue your work in a directory other than where the installation of anaconda is done, you can choose to change the default working directory of the Jupyter Notebook using the following steps:

Step 1: Go to cmd or anaconda prompt and type

Jupyter lab --generate-config

which creates a jupyter notebook config file in C:\users\username.jupyter

Step 2: Go to C:\users\username\.jupyter and open the jupyter config file which will be named “jupyter_lab_config”

Step 3: After opening the file search for this line (you can use ctrl+f)

c.ServerApp.root_dir = '' "

Step 4: Now when you find that line put the desired folder location inside the quote and don’t forget to uncomment it. It will look something like this

c.ServerApp.root_dir = 'c:/users/username/myfolder'
(Previously it would look like : c.ServerApp.root_dir = ' ')

Step 5: Save the file and restart the jupyter lab/notebook

If you are using an older version of Python or the IPython version, refer to this Stack Overflow thread to find more instructions on how you can change your working directory accordingly:

Conclusion

Finding your way around a Jupyter Notebook can be tricky if you come as someone new to coding or someone who is just more comfortable with using an IDE like Atom or Sublime Text, but using Jupyter Notebooks has its perks as the code does not have to be compiled to execute and you can work on your code line by line. Changing the working directory is an easily achievable task using the OS library with Python. It is also recommended that you use a Virtual Environment instead of the local system environment to avoid any changes to the system’s settings.

Try to change the working directory in a Jupyter Notebook using the method above and let us know in the comments below if it helped you.

Share this post

Read next...

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments