File Metadata
Retrieve basic file metadata like size and modification time.
IntermediateTopic: File Handling Programs
Python File Metadata Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# Program to get file metadata
import os
import time
filename = input("Enter filename: ")
try:
stats = os.stat(filename)
print("Size (bytes):", stats.st_size)
print("Last modified:", time.ctime(stats.st_mtime))
except FileNotFoundError:
print("File not found.")Output
Enter filename: notes.txt Size (bytes): 120 Last modified: Mon Nov 25 10:30:00 2025
Understanding File Metadata
os.stat returns an object with size, timestamps, and other low-level metadata; time.ctime renders timestamps nicely.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.