Python3 os.chdir() Method

Python3 OS 文件/目录方法Python3 OS File/Directory Methods


Overview

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

Syntax

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

os.chdir(path)

Parameters

  • path-- The new path to switch to.

Return Value

Returns True if access is allowed, otherwise returns False.

Examples

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

#!/usr/bin/python3

import os, sys

path = "/tmp"

# 查看当前工作目录
retval = os.getcwd()
print ("当前工作目录为 %s" % retval)

# 修改当前工作目录
os.chdir( path )

# 查看修改后的工作目录
retval = os.getcwd()

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

The output of the above program is:

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

Python3 OS 文件/目录方法Python3 OS File/Directory Methods

Other Extensions