import argparse
import datetime
import os
def create_date_environment(args):
"""
Creates a directory with date-based naming conventions and a sample date file.
"""
try:
# Validate date range
if args.start_date > args.end_date:
raise ValueError("Start date must be before end date.")
# Create directory name
date_str = args.start_date.strftime("%Y%m%d")
dir_name = f"date_env_{date_str}"
dir_path = os.path.join(".", dir_name) # Current directory
# Create directory if it doesn't exist
if not os.path.exists(dir_path):
os.makedirs(dir_path)
print(f"Created directory: {dir_path}")
else:
print(f"Directory already exists: {dir_path}")
# Create sample date file
file_path = os.path.join(dir_path, "sample_date.txt")
with open(file_path, "w") as f:
f.write(f"This is a sample date file for {args.start_date.strftime('%Y-%m-%d')}.")
print(f"Created sample date file: {file_path}")
except ValueError as e:
print(f"Error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Create a date-based environment.")
parser.add_argument("start_date", help="Start date in YYYY-MM-DD format")
parser.add_argument("end_date", help="End date in YYYY-MM-DD format")
args = parser.parse_args()
create_date_environment(args)
Add your comment