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.
Python code
21 linesimport 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
/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
- Use `datetime.datetime.utcnow().date().isoformat()` to avoid timezone issues in distributed systems
- 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
More from Data pipelines & processing
- Add a UUID Surrogate Key to Each Row in a CSV with Python easy
- Attach Source File Metadata to Records in Python easy
- Build a Python Utility That Detects Duplicate Records Across Multiple Excel Sheets medium
- Check Null Rate Threshold in PySpark DataFrame medium
- Count Records Processed per Category in Python easy
- Create Data Helper Functions in Python for Beginners easy
Keep learning
Related tutorials and quizzes for this topic.