Latest web development tutorials

Python os.chdir () method

Python File (File) method Python OS file / directory methods


Outline

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

grammar

chdir () method syntax is as follows:

os.chdir(path)

parameter

  • path - to switch to the new path.

return value

If you allow access to return True, otherwise False.

Examples

The following example demonstrates chdir () method of use:

#!/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 above program output is:

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

Python File (File) method Python OS file / directory methods