Python中的OS模块提供了与操作系统进行交互的函数。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的函数的便携式方法。
os.fchdir()
Python中的方法用于将当前工作目录更改为给定文件描述符表示的目录。
文件描述符是小整数值,与文件或其他输入/输出资源(例如管道或网络套接字)相对应。文件描述符是资源的抽象指示符,并充当执行各种较低级别I /O操作(如读取,写入,发送等)的句柄。
例如:标准输入通常是值为0的文件描述符,标准输出通常是值为1的文件描述符,标准错误通常是值为2的文件描述符。
当前进程打开的其他文件将获得值3、4、5,依此类推。
os.fchdir()
方法等效于os.chdir(file_descriptor)
方法。
用法: os.fchdir(fd)
参数:
fd:文件描述符。文件描述符必须代表打开的目录,而不是打开的文件。
返回类型:此方法不返回任何值。
代码1:使用os.fchdir()方法更改当前工作目录
# Python program to explain os.fchdir() method
# importing os module
import os
# Print the current working
# directory using os.getcwd() method
print("Current working directory:", os.getcwd())
# Path
path = "/home/ihritik/Documents"
# open the directory represented by
# the above given path and get
# the file descriptor associated
# with it using os.open() method
fd = os.open(path, os.O_RDONLY)
# Change the current working
# directory using os.fchdir() method
os.fchdir(fd)
print("Current working directory changed")
# Print the current working
# directory using os.getcwd() method
print("Current working directory:", os.getcwd())
输出:
Current working directory: /home/ihritik Current working directory changed Current working directory: /home/ihritik/Documents
代码2:使用os.fchdir()方法时可能出现的错误
# Python program to explain os.fchdir() method
# importing os module
import os
# Path
path = "/home/ihritik/Documents/file.txt"
# open the above path and get
# the file descriptor associated
# with it using os.open() method
fd = os.open(path, os.O_RDONLY)
# The file descriptor must
# represent an open file
# instead of an opened directory
# The method will raise
# 'NotADirectoryError' exception
# Change the current working
# directory using os.fchdir() method
os.fchdir(fd)
print("Current working directory changed")
# Print the current working
# directory using os.getcwd() method
print("Current working directory:", os.getcwd())
输出:
Traceback (most recent call last): File "changeDir.py", line 24, in os.fchdir(fd) NotADirectoryError: [Errno 20] Not a directory
代码3:使用os.fchdir()方法时可能的错误处理
# Python program to explain os.fchdir() method
# importing os module
import os
# Path
path = "/home/ihritik/Desktop/file.txt"
# try opening the above path and get
# the file descriptor associated
# with it using os.open() method
try :
fd = os.open(path, os.O_RDONLY)
# Try Changing the current working
# directory using os.fchdir() method
try :
os.fchdir(fd)
print("Current working directory changed")
# Print the current working
# directory using os.getcwd() method
print("Current working directory:", os.getcwd())
# Catch exceptions
# If file descriptor does
# not represents a directory
except NotADirectoryError:
print("The given file descriptor does \
not represent a directory")
# Catch exceptions
# If path does not exists
except FileNotFoundError:
print("Path does not exists")
# If there is any permission
# related issue while opening
# the given path
except PermissionError:
print("Permission denied")
输出:
The given file descriptor does not represent a directory
相关用法
- Python os.dup()用法及代码示例
- Python next()用法及代码示例
- Python set()用法及代码示例
- Python hasattr()用法及代码示例
- Python PIL putdata()用法及代码示例
- Python PIL getcolors()用法及代码示例
- Python PIL tobytes()用法及代码示例
- Python PIL getpalette()用法及代码示例
- Python os.get_blocking()用法及代码示例
- Python Tensorflow cos()用法及代码示例
- Python sympy.crt()用法及代码示例
- Python sympy.nT()用法及代码示例
- Python PIL putalpha()用法及代码示例
注:本文由纯净天空筛选整理自ihritik大神的英文原创作品 Python | os.fchdir() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。