What is a file descriptor used in Python?
File descriptors in Python are identifiers that represents the open files in the os kernel and are kept in a table of files. Typically, they have non-negative values. Negative results denote an error or a «no value» condition. They support a variety of file-related operations. In general, descriptors are a special approach Python uses to maintain attributes.
The main things they assist with are accessing files and other input/output devices like network sockets or pipes.
These operations take place on I/O streams identified by following file descriptors −
close( fd )
File descriptor closure. This function must be used with a file descriptor returned by open() or pipe() because it is meant for low-level I/O. Use the close() method of the file object returned by popen(), fdopen(), or the built-in function open() to close the file.
Example
Following is an example of 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)
Output
Following is an output of the above code −
The descriptor f the file for TutorialsPoint.txt is 3
dup(fd)
Returns duplicate file descriptor fd.
Example
Following is an example of 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")
Output
Following is an output of the above code −
file descriptor: 3 Duplicate file descriptor: 4 The file descriptor is duplicated successfully
dup2( fd , fd2 )
Duplicates the file descriptor fd to fd2, shutting the second one first if needed.
Example
Following is an example of 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")
Output
Following is an output of the above code −
file descriptor: 3 Duplicate file descriptor: 2 The file descriptor is duplicated successfully
fstat(fd)
Provides a status for the file descriptor fd, such as stat ().
Example
Following is an example of 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)
Output
Following is an output of the above code −
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]
Returns details, such as statvfs(), about the filesystem that contains the file associated with file descriptor fd.
Example
Following is an example of 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)
Output
Following is an output of the above code
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 )
Truncates the file associated with file descriptor fd, reducing its size to length bytes.
Example
Following is an example of 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)
Output
Following is an output of the above code
file’s size in bytes is: 206 This file’s size in bytes is: 5
lseek( fd , pos , how )
Sets file descriptor fd’s current position to position pos, modified by how: to set the position relative to the file’s beginning; to set it relative to the current position; and to set it relative to the file’s end.
Example
Following is an example of 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)
Output
Following is an output of the above code