Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected

How to Generate an Inventory Report of All Files in Python

Walk a directory tree, collect metadata for every file, and write a CSV inventory report using Python's os, pathlib, and csv modules.

Medium Python 3.9+ Jun 27, 2026 Files & data 1 views 0 copies

Python code

36 lines
Python 3.9+
import os
import csv
from pathlib import Path
from datetime import datetime

def generate_inventory_report(root_dir: str = "/", output_file: str = "inventory_report.csv"):
    headers = ["File Path", "Size (bytes)", "Last Modified", "File Type"]
    rows = []
    start_time = datetime.now()
    
    for dirpath, dirnames, filenames in os.walk(Path.home()):
        for filename in filenames:
            full_path = Path(dirpath) / filename
            try:
                stats = full_path.stat()
                rows.append([
                    str(full_path),
                    stats.st_size,
                    datetime.fromtimestamp(stats.st_mtime).isoformat(),
                    full_path.suffix.lower() if full_path.suffix else "no extension"
                ])
            except (OSError, PermissionError):
                continue
    
    with open(output_file, "w", newline="", encoding="utf-8") as f:
        writer = csv.writer(f)
        writer.writerow(headers)
        writer.writerows(rows)
    
    elapsed = (datetime.now() - start_time).total_seconds()
    print(f"Report generated: {output_file}")
    print(f"Files processed: {len(rows)}")
    print(f"Time elapsed: {elapsed:.2f} seconds")

if __name__ == "__main__":
    generate_inventory_report()

Output

stdout
Report generated: inventory_report.csv
Files processed: 15
Time elapsed: 0.12 seconds

How it works

os.walk yields directory and file names recursively, letting you iterate over an entire filesystem subtree. Path.stat() retrieves file size, modification time, and other metadata with a single system call. By catching OSError and PermissionError, the script skips inaccessible files without crashing. The csv.writer outputs the collected data as a structured CSV file. Timing with datetime.now() gives a simple performance metric.

Common mistakes

  • Hardcoding the root directory to '/' can cause permission errors or extremely long runtimes on large drives.
  • Forgetting to catch OSError and PermissionError leads to abrupt script crashes on locked or restricted files.
  • Using time.time() instead of datetime.now() for timing can be less readable when formatting as an ISO timestamp.

Variations

  1. Use pathlib.rglob('*') for a simpler recursive glob pattern instead of os.walk.
  2. Add filtering by file extension or minimum size to reduce output volume.

Real-world use cases

  • Auditing a server's filesystem to list all files and their sizes for storage capacity planning.
  • Generating a metadata manifest before migrating files between cloud storage buckets.
  • Creating a searchable index of file locations and last-modified dates for compliance reporting.

Sponsored

Sponsored Reserved space — layout preview until AdSense is connected

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Files & data

Related tutorials and quizzes for this topic.