首页 > Python资料 博客日记

Python os.fchdir()方法

2023-08-06 15:26:52Python资料围观182

这篇文章介绍了Python os.fchdir()方法,分享给大家做个参考,收藏Python资料网收获更多编程知识

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

语法

以下是fchdir()方法的语法 -

os.fchdir(fd)

参数

  • fd − 这是目录描述符。

返回值

  • 此方法返回文件描述符的副本。

示例

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

#!/usr/bin/python3
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

版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐