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,



Python Recommendations:

C++ & C++11 Recommendations:

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.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.