Python 3 - os.chdir() Method


Description

The method chdir() changes the current working directory to the given path.It returns None in all the cases.

Syntax

Following is the syntax for 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. It throws FileNotFoundError if specified path is not found

Example

The following example shows the usage of chdir() method.

#!/usr/bin/python3
import os

path = "d:\\python3" #change path for linux

# Now change the directory
os.chdir( path )

# Check current working directory.
retval = os.getcwd()

print ("Directory changed successfully %s" % retval)

Result

When we run the above program, it produces the following result −

Directory changed successfully d:\python3
python_files_io.htm
Advertisements