Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 os.chdir() Method

Python3 os.chdir() Method

Python3 OS File/Directory Methods Python3 OS File/Directory Methods


Overview

The os.chdir() method changes the current working directory to the specified path.

Syntax

The **chdir()** method syntax is as follows:


os.chdir(path)

Parameters

Return Value

Returns True if access is allowed, otherwise False.

Example

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


#!/usr/bin/python3

import os, sys

path = "/tmp"

# View current working directory
retval = os.getcwd()
print ("当前工作目录为 %s" % retval)

# Change current working directory
os.chdir( path )

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

print ("目录修改成功 %s" % retval)

The output of the above program is:


当前工作目录为 /www
目录修改成功 /tmp

Python3 OS File/Directory Methods Python3 OS File/Directory Methods