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.
Python code
36 linesimport 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
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
- Use pathlib.rglob('*') for a simpler recursive glob pattern instead of os.walk.
- 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
More from Files & data
- Build a Command-Line To-Do List Application with Data Persistence in Python easy
- Build a Python Script That Detects and Deletes Empty Files Across Folders easy
- Compare Two Folder Structures and Find Differences in Python easy
- Compress and Extract ZIP Files Programmatically in Python easy
- Convert CSV Files to JSON in Python easy
- Convert Image to ASCII Art in Python medium
Keep learning
Related tutorials and quizzes for this topic.