Skip to main content

fileno()

The fileno() function, belonging to the File Methods in Python, is used to return the integer file descriptor associated with the file object. This file descriptor can be used in lower-level I/O operations.

Parameter Values

This function does not accept any parameters.

Return Values

The fileno() method returns an integer file descriptor if successful.

How to Use fileno() in Python

Example 1:

The fileno() method returns the integer file descriptor associated with the file object.

file = open('example.txt', 'r')
print(file.fileno())
Example 2:

If the file object was not created by Python os.open(), it will raise OSError when invoked.

file = open('example.txt', 'r')
print(file.fileno())
Example 3:

This method is commonly used to obtain the file descriptor for low-level operations.

file = open('example.txt', 'r')
print(file.fileno())