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 '
廣告
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP