Print Current Working Directory in Jupyter Notebook

This tutorial shows how you can print out the current working directory in Jupyter Notebook. In particular, you can use os.getcwd() to do it and this tutorial provides the complete Python code.

In particular, the following is the Python code. The OS module provides functions for interacting with the operating system in Python and thus we first import this module. Then, we use the getcwd() to print out the working directory. cwd stands for Current Working Directory(CWD).  

# Python code to import os module:
import os

# Python code to print out the current working directory
os.getcwd()

After running the Python code above, you can see the current working directory in the output. If you call files without specifying path, Python assumes that it starts in the CWD. If the called files is not in the current working directory, it will return errors.

For instance, in Jupyter notebook, if you try to read csv file “hypothetical_data.csv” but it is not in the current working directory. It will return the error like the one below.

# import pandas module
import pandas as pd

# try to read the csv file
sample_file = pd.read_csv ('hypothetical_data.csv')

The following is the error. The solution is either to change the working directory or to move the CSV file into the current working directory.

FileNotFoundError: [Errno 2] File hypothetical_data.csv does not exist: 'hypothetical_data.csv'