Files, directories, sockets, and so on, are all files in Linux. Every file has a non-negative integer associated with it. This non-negative integer is called the file descriptor for that particular file. File descriptors are allocated in sequential order, with the lowest possible unallocated positive integer value taking precedence.
The following file descriptors are always opened whenever a program is run/executed.
0.1.2.fileno methodIn Python, fileno is an instance method on the file object that returns the file descriptor for the given file.
fileObject.fileno()
This method has no parameters.
This method returns the file descriptor of the particular fileobject.
import socketf_name = "file.txt"fileObject = open(f_name, "r")print("The file descriptor for %s is %s" % (f_name, fileObject.fileno()))sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)print("The file descriptor for TCP socket is %s" % (sock.fileno()))
In the code above, we create a file called file.txt.
socket package.f_name that holds the file name.open() with the read mode (r).fileno method to obtain the file descriptor for the file object.socket() method to create a TCP socket called sock.fileno method to obtain the file descriptor for the sock object obtained in Line 8.Note: Refer to Socket programming in Python to learn more socket programming in Python.