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.
fileno is an instance method on the file object that returns the file descriptor for the given file.
This method has no parameters.
This method returns the file descriptor of the particular
import socket f_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
f_namethat holds the file name.
open()with the read mode (
filenomethod to obtain the file descriptor for the file object.
socket()method to create a TCP socket called
filenomethod to obtain the file descriptor for the
sockobject obtained in Line 8.
Note: Refer to Socket programming in Python to learn more socket programming in Python.
View all Courses