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

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

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

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

技术教程推荐

推荐系统三十六式 -〔刑无刀〕

趣谈网络协议 -〔刘超〕

数据结构与算法之美 -〔王争〕

Elasticsearch核心技术与实战 -〔阮一鸣〕

系统性能调优必知必会 -〔陶辉〕

打造爆款短视频 -〔周维〕

玩转Vue 3全家桶 -〔大圣〕

Serverless进阶实战课 -〔静远〕

JavaScript进阶实战课 -〔石川〕

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