In this article we will discuss how to change the current working directory in python.

Current working directory is the directory in which program is running.

Change Current Working Directory in Python

Python’s os module provides a function to change the current working directory i.e.

It changes the current working directory to the given path.

Let’s understand by an example,

First print the current working directory using os.getcwd() i.e.

Now let’s change the current working directory using os.chdir(path) i.e.

If the given path don’t exist then os.chdir() with throw error : FileNotFoundError. Therefore we should either call it using try / except i.e.

or check if the new directory exists before changing the working directory i.e.

Complete example is as follows,

Output:

 

Click Here to Subscribe for more Articles / Tutorials like this.