chdir method of os module in Python

The os.chdir() method in Python changes the current working directory of the process. The Python example prints the current working directory and changes it subsequently using os.chdir(), to the $HOME directory of the user and prints it.

Os Updated 16 Nov 2018 1 min read

Method Name

os.chdir

Method signature

os.chdir()

Return Value

None

Overview

  • The current working directory of a process can be changed dynamically by calling os.chdir().
  • The current working directory of a process is a directory that is assigned to the process using which references to any file can be made using it without the need to qualify the whole directory hierarchy.
  • The current working directory is also called as the present working directory or simply the working directory.
  • The current working directory can be printed using method os.getcwd().

Example

python
import os

print("Current working directory:")

print(os.getcwd())

print("Changing the working directory to $Home directory")

os.chdir(os.environ['HOME'])

print("The Current working directory now is:")

print(os.getcwd())

Output

Output
Current working directory:

/examples/os

Changing the working directory to $Home directory

The Current working directory now is:

/Users/python