Python Practice › OS Module Programs › Program 48
File details with os.stat()
Write a Python program to show the details of a file with os.stat().
Output
size: 5 is regular file: True has mtime: True
Approach
stat()returns size, timestamps and mode.
Solution
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.