File Metadata in Python
Retrieve basic file metadata like size and modification time.
IntermediateFile Handling ProgramsExample 17 of 20
file-metadata.py
1# Program to get file metadata23import os4import time56filename = input("Enter filename: ")78try:9 stats = os.stat(filename)10 print("Size (bytes):", stats.st_size)11 print("Last modified:", time.ctime(stats.st_mtime))12except FileNotFoundError:13 print("File not found.")
Output
Enter filename: notes.txt Size (bytes): 120 Last modified: Mon Nov 25 10:30:00 2025
What's going on
os.stat returns an object with size, timestamps, and other low-level metadata; time.ctime renders timestamps nicely.
After this program
If the idea is still fuzzy, read Day 14: File Handling in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 20 file handling programs, or see every topic on Python exercises.