Python os.fchdir() - 将当前工作目录更改为由给定文件描述符表示的目录

Python os.fchdir()

Python中的os.fchdir()方法用于将当前工作目录更改为由给定文件描述符表示的目录

文件描述符是一个小整型值,对应于一个文件或其他输入/输出资源,如管道或网络套接字。一个文件描述符是一个资源的抽象指示器,并作为句柄来执行各种低级的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教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程