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 '