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()方法的用法。

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

来源:LearnFk无涯教程网

#!/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

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

技术教程推荐

软件工程之美 -〔宝玉〕

分布式金融架构课 -〔任杰〕

如何成为学习高手 -〔高冷冷〕

程序员的个人财富课 -〔王喆〕

徐昊 · TDD项目实战70讲 -〔徐昊〕

Dubbo源码剖析与实战 -〔何辉〕

现代C++20实战高手课 -〔卢誉声〕

结构思考力 · 透过结构看表达 -〔李忠秋〕

AI 应用实战课 -〔黄佳〕

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