1. import os
  2. def extend_file_path_for_diagnostics(file_path, diagnostic_level="INFO"):
  3. """
  4. Extends a file path for diagnostics by adding a diagnostic level indicator.
  5. Args:
  6. file_path (str): The original file path.
  7. diagnostic_level (str, optional): The diagnostic level (e.g., "INFO", "WARNING", "ERROR"). Defaults to "INFO".
  8. Returns:
  9. str: The extended file path with the diagnostic level.
  10. """
  11. # Ensure the file path is a string
  12. file_path = str(file_path)
  13. # Extract the directory from the file path
  14. directory = os.path.dirname(file_path)
  15. # Extract the file name from the file path
  16. file_name = os.path.basename(file_path)
  17. # Create the diagnostic level string
  18. diagnostic_string = f"[{diagnostic_level}]"
  19. # Construct the extended file path
  20. extended_file_path = os.path.join(directory, diagnostic_string + file_name)
  21. return extended_file_path
  22. if __name__ == '__main__':
  23. # Example Usage
  24. original_path = "my_data/report.txt"
  25. extended_path = extend_file_path_for_diagnostics(original_path, "DEBUG")
  26. print(f"Original path: {original_path}")
  27. print(f"Extended path: {extended_path}")
  28. original_path = "/path/to/log.file"
  29. extended_path = extend_file_path_for_diagnostics(original_path) # uses default INFO level
  30. print(f"Original path: {original_path}")
  31. print(f"Extended path: {extended_path}")

Add your comment