Python os.chdir() Method
Python OS File/Directory Methods
Overview
The os.chdir() method is used to change the current working directory to the specified path.
Syntax
chdir()The method syntax format is as follows:
os.chdir(path)
Parameters
pathpath -- The new path to switch to.
Return Value
Returns True if access is allowed, otherwise returns False.
Example
The following example demonstrates the use of the chdir() method:
#!/usr/bin/python # -*- coding: UTF-8 -*- 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 execution is:
当前工作目录为 /www 目录修改成功 /tmpOther Extensions