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

First of all we need to import python’s os module i.e.

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:

 

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,