AutomationResearched. Verified against official documentation (2 sources), not personally tested.

Number and date every file a script produces

A short Python script that adds today's date and a running number to every output file, so runs never overwrite each other.

1 min read

A script that saves its output to the same filename every time erases yesterday’s work. Run it twice and you only keep the last copy. Adding a date and a running number means each run leaves its own file behind.

Save to a new file every time

Use Python’s datetime module (it gives you the current date) and pathlib (it lets you create folders and write files). Both come with Python, so there is nothing to install.

This is like writing today’s date on a folder, then numbering the pages inside it Page 1, Page 2 so nothing gets lost.

  1. Open a file called save_output.py.
  2. Paste this code:
from datetime import date
from pathlib import Path

today = date.today().isoformat()
folder = Path("outputs")
folder.mkdir(exist_ok=True)

existing = list(folder.glob(f"report-{today}-*.txt"))
next_number = len(existing) + 1

filename = folder / f"report-{today}-{next_number}.txt"
filename.write_text("Your script output goes here.")
print(f"Saved {filename}")

isoformat writes the date as year-month-day. glob looks for files that match a pattern. mkdir(exist_ok=True) creates the folder only if it is not already there.

  1. Run it:
python save_output.py

Reference: datetime — Basic date and time types Reference: pathlib — Object-oriented filesystem paths

Expected result

The script creates an outputs folder and saves a file like report-2026-05-20-1.txt, with today’s real date in place of 2026-05-20. Run it again later the same day and the number becomes 2. Tomorrow the number starts again at 1.

Sources

  1. https://docs.python.org/3/library/datetime.html
  2. https://docs.python.org/3/library/pathlib.html

Stay Ahead. Work Smarter.

Get practical AI tips, automation ideas and useful technology tricks — one useful thing at a time. Follow the feed in any reader.