Interpreting stat() results using Python


The stat() method is part of OS module which describes various OS related operations on files and directories. For example, if we want to know various user defined flags for a file or size of the file in bytes.

Functions in os.stat() module

Below is a list of some sample functions available in stat() and their meaning.

  • st_size − It represents the size of the file in bytes.

  • st_atime − It represents the time of most recent access. It is expressed in seconds.

  • st_ctime − It represents the time of most recent metadata change on Unix and creation time on Windows. It is expressed in seconds.

  • st_blocks − It represents the number of 512-byte blocks allocated for file.

  • st_uid − It represents the user identifier of the file owner.

  • st_gid − It represents the group identifier of the file owner.

  • st_dev − It represents the identifier of the device on which this file resides.

  • st_flags  − It represents the user defined flags for file.

Example

In the below program we will see how some of the above functions are used.

import os

# Choose a file
path = 'E:\customers.csv'

# Get the status
status = os.stat(path)

# Print the result
print(status)

Output

Running the above code gives us the following result −

os.stat_result(st_mode=33206, st_ino=1125899906970419, st_dev=1614938858, st_nlink=1, st_uid=0, st_gid=0, st_size=261693, st_atime=1593271710, st_mtime=1593271710, st_ctime=1593271710)

Updated on: 10-Jul-2020

250 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements