OS File 中的 os.chdir(path)函数

首页 / Python2入门教程 / OS File 中的 os.chdir(path)函数

Python方法chdir()将当前工作目录更改为给定的路径。在所有情况下都不返回。

os.chdir(path) - 语法

os.chdir(path)
  • path  -  这是要更改到新位置的目录的完整路径。

os.chdir(path) - 示例

以下示例显示了chdir()方法的用法。

#!/usr/bin/python
import os

path="/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

当无涯教程运行上面的程序时,它产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/python/os-chdir.html

来源:LearnFk无涯教程网

Current working directory /usr
Directory changed successfully /usr/tmp

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

快速上手Kotlin开发 -〔张涛〕

深入浅出计算机组成原理 -〔徐文浩〕

Linux实战技能100讲 -〔尹会生〕

张汉东的Rust实战课 -〔张汉东〕

爱上跑步 -〔钱亮〕

人人都用得上的写作课 -〔涵柏〕

计算机基础实战课 -〔彭东〕

JavaScript进阶实战课 -〔石川〕

AI大模型之美 -〔徐文浩〕

好记忆不如烂笔头。留下您的足迹吧 :)