code: def _wrap_path_or_stream(method, mode):  # noqa: D202
      
  
      def decorator(path_or_stream):
          """Open the path if needed."""
          if isinstance(path_or_stream, (str, Path)):
              return method(Path(path_or_stream).open(mode))
          return method(path_or_stream)
  
      return decorator
docs: Open path with context or close stream at the end.