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

Python os.chdir() method

Python os.chdir() method

Python File methods Python 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/python
# -*- coding: UTF-8 -*-

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

Python File methods Python OS file/directory methods