當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python os.fchdir()用法及代碼示例


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


相關用法


注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | os.fchdir() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。