Python os.chdir() 方法

Python 教程 · 2019-02-03 17:12:59

概述

os.chdir() 方法用于改变当前工作目录到指定的路径。

语法

chdir()方法语法格式如下:

os.chdir(path)

参数

  • path -- 要切换到的新路径。

返回值

如果允许访问返回 True , 否则返回False。

实例

以下实例演示了 chdir() 方法的使用:

#!/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

执行以上程序输出结果为:

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

点击查看所有 Python 教程 文章: https://codercto.com/courses/l/8.html

查看所有标签

Programming Ruby

Programming Ruby

Dave Thomas、Chad Fowler、Andy Hunt / Pragmatic Bookshelf / 2004-10-8 / USD 44.95

Ruby is an increasingly popular, fully object-oriented dynamic programming language, hailed by many practitioners as the finest and most useful language available today. When Ruby first burst onto the......一起来看看 《Programming Ruby》 这本书的介绍吧!

html转js在线工具
html转js在线工具

html转js在线工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具