How to create a dated snapshot path for a dataset in Python

Generate a versioned directory path combining a base directory, dataset name, and today's date, ready for creating snapshots in data pipelines.

Easy Python 3.9+ Aug 9, 2026 Data pipelines & processing 15 views 0 copies

Python code

21 lines
Python 3.9+
import datetime
import os
from pathlib import Path


def snapshot_path(base_dir: str, dataset_name: str) -> Path:
    """Return a dated snapshot path for a dataset under a base directory."""
    today = datetime.date.today().isoformat()
    return Path(base_dir) / dataset_name / today


if __name__ == "__main__":
    # Example usage with a temporary path
    base = "/data/projects"
    dataset = "customer_orders"
    path = snapshot_path(base, dataset)
    print(path)

    # Show the directory is created (commented out to avoid side effects)
    # path.mkdir(parents=True, exist_ok=True)
    # print("Created:", path)

Output

stdout
/data/projects/customer_orders/2025-04-10

How it works

This function uses datetime.date.today().isoformat() to produce a sortable YYYY-MM-DD string, which naturally orders snapshots by date. pathlib.Path builds the nested path with the / operator, keeping the code clean across platforms. Creating the directory is left to the caller so this helper stays side-effect free. Using an ISO date keeps paths unambiguous and compatible with standard date parsing tools.

Common mistakes

  • Not calling `.mkdir(parents=True, exist_ok=True)` before writing files to the path
  • Using `datetime.now()` instead of `datetime.date.today()` and getting time components in the name
  • Hardcoding the date or using a non-sortable format like `%d-%m-%Y`

Variations

  1. Use `datetime.datetime.utcnow().date().isoformat()` to avoid timezone issues in distributed systems
  2. Add a timestamp: `datetime.datetime.now().strftime('%Y%m%d_%H%M%S')` for more granular snapshots

Real-world use cases

  • Writing daily batch output files to a versioned S3 prefix or local directory in an ETL pipeline.
  • Storing model training data snapshots so each run uses a reproducible dataset version.
  • Creating dated backup folders when archiving logs or database exports in a scheduled job.

Sponsored

Run this sample

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

Open editor

More from Data pipelines & processing

Related tutorials and quizzes for this topic.