136 of 151 menu

The stat method of the os module

The stat method is designed to obtain information about the status of the path specified in its parameter. Returns a string or a byte object os.stat_result with attributes.

Attributes of the os.stat object_result

Attribute Description
st_mode Shows the file type and file bit mode.
st_ino Shows the inode number on Unix and the file index on Windows.
st_dev Shows the device ID where the file is located.
st_nlink Shows the number of hard links.
st_uid Shows the user ID of the file owner.
st_gid Shows the group ID of the file owner.
st_size Shows the file size in bytes.
st_atime Shows the last access time. Expressed in seconds.
st_mtime Shows the time of the last modification of the content. Expressed in seconds.
st_ctime Shows the last modification time of metadata on Unix and creation time on Windows. Expressed in seconds.
st_atime_ns Shows the last access time. Expressed in nanoseconds as an integer.
st_mtime_ns Shows the time of the last modification of the content. Expressed in nanoseconds as an integer.
st_ctime_ns Shows the last modification time of metadata on Unix and creation time on Windows. Expressed in nanoseconds as an integer.
st_blocks Shows the number of 512 byte blocks allocated for the file.
st_rdev Shows the device type if it is an inode.
st_flags Shows user-defined flags for a file.

Syntax

import os os.stat(file path/folder)

Example

Let's find out the status of the file:

import os print(os.stat('file.txt'))

The result of the executed code:

os.stat_result( st_mode=33206, st_ino=1970324838088376, st_dev=2862826519, st_nlink=1, st_uid=0, st_gid=0, st_size=210, st_atime=1701422724, st_mtime=1701364277, st_ctime=1701363216 )

See also

  • method path.getsize of module os,
    which returns the size of the path in bytes
  • method disk_usage of module shutil,
    which returns disk usage statistics
  • method getcwd module os,
    which returns the current working directory
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline