OS File 中的 os.fchdir(fd)函数

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

Python方法fchdir()将当前工作目录更改为文件描述符fd表示的目录。描述符必须引用打开的目录,而不是打开的文件。

os.fchdir(fd) - 语法

os.fchdir(fd);
  • fd  -  这是目录描述符。

os.fchdir(fd) - 示例

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

#!/usr/bin/python

import os, sys

# First go to the "/var/www/html" directory
os.chdir("/var/www/html" )

# Print current working directory
print "Current working dir : %s" % os.getcwd()

# Now open a directory "/tmp"
fd=os.open( "/tmp", os.O_RDONLY )

# Use os.fchdir() method to change the dir
os.fchdir(fd)

# Print current working directory
print "Current working dir : %s" % os.getcwd()

# Close opened directory.
os.close( fd )

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

无涯教程网

Current working dir : /var/www/html
Current working dir : /tmp

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

技术教程推荐

雷蓓蓓的项目管理实战课 -〔雷蓓蓓〕

NLP实战高手课 -〔王然〕

检索技术核心20讲 -〔陈东〕

Service Mesh实战 -〔马若飞〕

TensorFlow 2项目进阶实战 -〔彭靖田〕

WebAssembly入门课 -〔于航〕

如何读懂一首诗 -〔王天博〕

快手 · 移动端音视频开发实战 -〔展晓凯〕

手把手带你搭建推荐系统 -〔黄鸿波〕

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