1. import argparse
  2. import datetime
  3. import os
  4. def create_date_environment(args):
  5. """
  6. Creates a directory with date-based naming conventions and a sample date file.
  7. """
  8. try:
  9. # Validate date range
  10. if args.start_date > args.end_date:
  11. raise ValueError("Start date must be before end date.")
  12. # Create directory name
  13. date_str = args.start_date.strftime("%Y%m%d")
  14. dir_name = f"date_env_{date_str}"
  15. dir_path = os.path.join(".", dir_name) # Current directory
  16. # Create directory if it doesn't exist
  17. if not os.path.exists(dir_path):
  18. os.makedirs(dir_path)
  19. print(f"Created directory: {dir_path}")
  20. else:
  21. print(f"Directory already exists: {dir_path}")
  22. # Create sample date file
  23. file_path = os.path.join(dir_path, "sample_date.txt")
  24. with open(file_path, "w") as f:
  25. f.write(f"This is a sample date file for {args.start_date.strftime('%Y-%m-%d')}.")
  26. print(f"Created sample date file: {file_path}")
  27. except ValueError as e:
  28. print(f"Error: {e}")
  29. except Exception as e:
  30. print(f"An unexpected error occurred: {e}")
  31. if __name__ == "__main__":
  32. parser = argparse.ArgumentParser(description="Create a date-based environment.")
  33. parser.add_argument("start_date", help="Start date in YYYY-MM-DD format")
  34. parser.add_argument("end_date", help="End date in YYYY-MM-DD format")
  35. args = parser.parse_args()
  36. create_date_environment(args)

Add your comment