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

查看所有标签

Tales from Facebook

Tales from Facebook

Daniel Miller / Polity Press / 2011-4-1 / GBP 55.00

Facebook is now used by nearly 500 million people throughout the world, many of whom spend several hours a day on this site. Once the preserve of youth, the largest increase in usage today is amongst ......一起来看看 《Tales from Facebook》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具