Python os.fstat() Method

Python os.fstat() Method

  1. Syntax of Python os.fstat() Method
  2. Example 1: Use os.fstat() to Get Status of a File
  3. Example 2: Use os.stat() to Get Status of a File

The fstat() method is a part of the os module available in Python. The fstat() method gets the status of a file descriptor.

A file descriptor is an integer variable used to track an open file and perform several operations such as reading, writing, appending, deleting, etc. Additionally, in Python 3.3 and above, os.fstat() method is equivalent to os.stat() method.

Syntax of Python os.fstat() Method

os.fstat(fd)

Parameters

Type Description
fd integer A file descriptor associated with a valid resource.

Returns

The fstat() method returns a stat_result object that represents the state of a file descriptor. The stat_result is a class.

The following are some of the essential attributes of the stat_result class, along with some details.

Attribute Description
st_mode The file type and file permissions or mode bits.
st_ino The file index or the inode number on Windows and Linux, respectively.
st_dev An identifier for the system on which the file exists.
st_nlink Number of hard links of the opened file.
st_uid The user identifier for the opened file’s owner.
st_gid The group identifier for the opened file’s owner.
st_size File’s size in bytes. In the case of a symbolic link, it is the length of the file system path.
st_atime Most recent access time in seconds.
st_mtime Most recent modification time in seconds.
st_ctime Creation time in seconds on Windows and most recent metadata modification time on Unix in seconds.
st_atime_ns Most recent access time in nanoseconds.
st_mtime_ns Most recent modification time in nanoseconds.
st_ctime_ns Creation time in nanoseconds on Windows and most recent metadata modification time on Unix in nanoseconds.

Apart from the attributes stated above, a few more attributes are platform dependent and may not be available in all operating systems.

To learn about them, refer to the official documentation here.

Example 1: Use os.fstat() to Get Status of a File

import os

fd = os.open("file.txt", os.O_CREAT) # Create file if it doesn't exist
print(os.fstat(fd))
os.close(fd)

Output:

os.stat_result(
    st_mode=33261,
    st_ino=1444089,
    st_dev=176,
    st_nlink=1,
    st_uid=100,
    st_gid=101,
    st_size=0,
    st_atime=1660144940,
    st_mtime=1660144940,
    st_ctime=1660144940
)

The Python code above uses the os.open() method to read a file. It creates the file if it doesn’t exist.

The output prints a stat_result object along with various attributes we discussed and their respective values.

Example 2: Use os.stat() to Get Status of a File

import os

fd = os.open("file.txt", os.O_CREAT) # Create file if it doesn't exist
print(os.stat(fd))
os.close(fd)

Output:

os.stat_result(
    st_mode=33261,
    st_ino=1444160,
    st_dev=149,
    st_nlink=1,
    st_uid=100,
    st_gid=101,
    st_size=0,
    st_atime=1660144953,
    st_mtime=1660144953,
    st_ctime=1660144953
)

As mentioned above, the os.stat() method is equivalent to os.fstat() method. Hence, the output is the same.

Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

LinkedIn GitHub

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.set_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_handle_inheritable Method
  • Python os.get_inheritable Method