Python方法chdir()将当前工作目录更改为给定的路径。在所有情况下都不返回。
os.chdir(path) - 语法
os.chdir(path)
path - 这是要更改到新位置的目录的完整路径。
os.chdir(path) - 示例
以下示例显示了chdir()方法的用法。
#!/usr/bin/python import ospath="/usr/tmp"
# Check current working directory. retval=os.getcwd() print "Current working directory %s" % retval
# Now change the directory os.chdir( path )
# Check current working directory. retval=os.getcwd()
print "Directory changed successfully %s" % retval
当无涯教程运行上面的程序时,它产生以下输出-
Current working directory /usr Directory changed successfully /usr/tmp