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 '
广告