Python os.chdir() Method



The Python os.chdir() method changes the current working directory to the given path. This command is sometimes used as an alias for the cd shell command on some system.

The directory in which all commands are being performed is known as the current working directory. It can be a database file, a folder, a library, or a directory. The current working directory is also known as the present working directory or just the working directory. This method does not return any value.

Syntax

Following is the syntax of Python os.chdir() method −

os.chdir(path)

Parameters

  • path − This is complete path of the directory to be changed to a new location.

Return Value

This method does not return any value.

Example

The following example shows the usage of Python os.chdir() method. Here we are changing the current working directory to the given path "/usr/tmp".

import os
path = "/usr/tmp"
# change the directory
x = os.chdir(path)
print ("Directory changed successfully",x)

When we run above program, it produces following result −

Directory changed successfully /usr/tmp

Example

In here, we are using the getcwd() method to know the current working directory of the file. After changing the path of the file, we are verifying the path of the current working directory using the os.chdir() method.

# importing the module
import os
# changing the current working directory to the given path
os.chdir('D:\Sarika Work')
# verifying the path using getcwd()
current = os.getcwd()
# printing the current working directory
print("The current working directory is:", current)

While executing the above code we get the following output −

The current working directory is: D:\Sarika Work

Example

If the directory requested does not exist, then this method raises a FileNotFoundError.

# importing the modules
import sys
import os
# the current directory
cd = os.getcwd()
# some false directory
nd = 'nonexist_dir / temp'
# trying to insert to the non existing directory
try:
   os.chdir(nd)
   print("Changing the path to:", os.getcwd())
# Caching the exception
except:
   print("Something went wrong with the given directory", sys.exc_info())
# handling with finally		
finally:
   print("Restore the path")
   os.chdir(cd)
   print("The current working directory is:", os.getcwd())

Following is an output of the above code −

Something went wrong with the given directory (<class 'FileNotFoundError'>, FileNotFoundError(2, 'The system cannot find the path specified'), <traceback object at 0x00000152D60B5640>)
Restore the path
The current working directory is: C:\Users\Lenovo\Desktop

Example

Now, we are changing the current working directory to the home directory.

import os
print("The current working directory is:", os.getcwd())
# Changing the working directory to home directory
x = os.chdir(os.path.expanduser('~'))
print("The Current directory now is:",os.getcwd())

Output of the above code is as follows −

The current working directory is: C:\Users\Lenovo\Desktop
The Current directory now is: C:\Users\Lenovo
os_file_methods.htm
Advertisements