File details with os.stat()

EasyOS Module Programs · 48 of 49

Write a Python program to show the details of a file with os.stat().

Advertisement
Output
size: 5
is regular file: True
has mtime: True

Approach

  1. stat() returns size, timestamps and mode.

Solution

Python
import osimport stat with open("info.txt", "w") as f:    f.write("hello")st = os.stat("info.txt")print("size:", st.st_size)print("is regular file:", stat.S_ISREG(st.st_mode))print("has mtime:", st.st_mtime > 0)
Output
size: 5
is regular file: True
has mtime: True

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Leave a Comment