Python 中的檔案描述符是什麼?


Python 中的檔案描述符是識別符號,**表示作業系統核心中開啟的檔案**,並儲存在檔案表中。通常,它們具有非負值。負結果表示錯誤或“無值”條件。它們支援各種與檔案相關的操作。通常,描述符是 Python 用於維護屬性的一種特殊方法。

它們主要用於訪問檔案和其他輸入/輸出裝置,例如網路套接字或管道。

這些操作在以下檔案描述符標識的 I/O 流上進行 -

close( fd )

檔案描述符關閉。此函式必須與 open() 或 pipe() 返回的檔案描述符一起使用,因為它用於低階 I/O。使用 popen()、fdopen() 或內建函式 open() 返回的檔案物件的 close() 方法關閉檔案。

示例

以下是 close(fd) 的示例 -

import os file = "TutorialsPoint.txt" file_Object = open(file, "r") fd = file_Object.fileno() print("The descriptor f the file for %s is %s" % (file, fd)) os.close(fd)

輸出

以下是上述程式碼的輸出 -

The descriptor f the file for TutorialsPoint.txt is 3

dup(fd)

返回重複的檔案描述符 fd。

示例

以下是 dup(fd) 的示例 -

import os file = "TutorialsPoint.txt" # opening the file and getting the file descriptor fd = os.open(file, os.O_WRONLY) print("file descriptor:", fd) # Duplicating the file descriptor dup_fd = os.dup(fd) print("Duplicate file descriptor:", dup_fd) # Closing the file descriptors os.close(fd) os.close(dup_fd) print("The file descriptor is duplicated successfully")

輸出

以下是上述程式碼的輸出 -

file descriptor: 3
Duplicate file descriptor: 4
The file descriptor is duplicated successfully

dup2( fd , fd2 )

將檔案描述符 fd 複製到 fd2,如果需要,先關閉第二個。

示例

以下是 dup2(fd,fd2) 的示例 -

import os file = "TutorialsPoint.txt" # opening the file and getting the file descriptor fd = os.open(file, os.O_WRONLY) print("file descriptor:", fd) # Duplicating the file descriptor dup_fd = 2 os.dup2(fd, dup_fd) print("Duplicate file descriptor:", dup_fd) # Closing the file descriptors os.close(fd) os.close(dup_fd) print("The file descriptor is duplicated successfully")

輸出

以下是上述程式碼的輸出 -

file descriptor: 3
Duplicate file descriptor: 2
The file descriptor is duplicated successfully

fstat(fd)

提供檔案描述符 fd 的狀態,例如 stat()。

示例

以下是 fstat(fd) 的示例 -

import os file = "TutorialsPoint.txt" # opening the file and getting the file descriptor fd = os.open(file, os.O_RDONLY) print("file descriptor:", fd) # Getting the status of the file descriptor s = os.fstat(fd) print(s) # Closing the file descriptors os.close(fd)

輸出

以下是上述程式碼的輸出 -

file descriptor: 3
os.stat_result(st_mode=33206, st_ino=10414574138731297, st_dev=2633115012, st_nlink=1, st_uid=0, st_gid=0, st_size=206, st_atime=1659955305, st_mtime=1659346059, st_ctime=1659343324)

fstatvfs(fd) [UNIX]

返回包含與檔案描述符 fd 關聯的檔案的檔案系統的詳細資訊,例如 statvfs()。

示例

以下是 fstatvfs(fd) 的示例 -

import os file = "TutorialsPoint.txt" # opening the file and getting the file descriptor fd = os.open(file, os.O_WRONLY) print("file descriptor:", fd) # Getting the info of the file with the file descriptor s = os.fstatvfs(fd) print(s) # Closing the file descriptors os.close(fd)

輸出

以下是上述程式碼的輸出

file descriptor: 3 os.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=15156185, f_bfree=9870528, f_bavail=9092519, f_files=3874816, f_ffree=3313786, f_favail=3313786, f_flag=4096, f_namemax=255)

ftruncate( fd, length )

截斷與檔案描述符 fd 關聯的檔案,將其大小縮減為 length 位元組。

示例

以下是 ftruncate(fd,length) 的示例 -

import os file = "TutorialsPoint.txt" # opening the file and getting the file descriptor fd = os.open(file, os.O_RDWR) # Printing the original file's size print("file’s size in bytes is:", os.stat(fd).st_size) # Length of the file in Bytes to which the file has to be truncated l = 5 os.ftruncate(fd, l) # Printing the content of the file size = os.stat(fd).st_size print(os.read(fd, size).decode("utf-8")) # Print the size of file (in bytes) print("file’s size in bytes is:", os.stat(fd).st_size)

輸出

以下是上述程式碼的輸出

file’s size in bytes is: 206
This
file’s size in bytes is: 5

lseek( fd , pos , how )

將檔案描述符 fd 的當前位置設定為位置 pos,由 how 修改:將位置設定為相對於檔案的開頭;將位置設定為相對於當前位置;並將位置設定為相對於檔案的末尾。

示例

以下是 lseek(fd,pos,how) 的示例

import os file = "TutorialsPoint.txt" # opening the file and getting the file descriptor fd = os.open(file, os.O_RDWR|os.O_CREAT) # Providing the string string = 'Welcome to Tutorials Point....Thank You!' #Convert the string in bytes l = str.encode(string) #Writing the string to the file os.write(fd,l) #Seek the file from start os.lseek(fd,0,0) #Reading the file string = os.read(fd, 11) print(string) os.close(fd)

輸出

以下是上述程式碼的輸出

b'Welcome to '

更新於: 2022-11-14

5K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.