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

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

技术教程推荐

零基础学Python -〔尹会生〕

趣谈Linux操作系统 -〔刘超〕

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

编辑训练营 -〔总编室〕

苏杰的产品创新课 -〔苏杰〕

跟月影学可视化 -〔月影〕

HarmonyOS快速入门与实战 -〔QCon+案例研习社〕

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

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

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