Chdir method of os module in Python

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:

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:

Current working directory:

/examples/os

Changing the working directory to $Home directory

The Current working directory now is:

/Users/python

 

 


Copyright 2024 © pythontic.com