Easy Tutorial
❮ Python String Min Python Array Rotation ❯

Python3 os.chdir() Method

Python3 OS File/Directory Methods


Overview

The os.chdir() method is used to change the current working directory to the specified path.

Syntax

The syntax for the chdir() method is as follows:

os.chdir(path)

Parameters

Return Value

Returns True if access is allowed, otherwise returns False.

Example

The following example demonstrates the use of the chdir() method:

#!/usr/bin/python3

import os, sys

path = "/tmp"

# View the current working directory
retval = os.getcwd()
print ("Current working directory is %s" % retval)

# Modify the current working directory
os.chdir( path )

# View the modified working directory
retval = os.getcwd()

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

Executing the above program will output:

Current working directory is /www
Directory changed successfully /tmp

Python3 OS File/Directory Methods

❮ Python String Min Python Array Rotation ❯