stouputils.print.output_stream module#

class TeeMultiOutput(
*files: IO[Any],
strip_colors: bool = True,
ascii_only: bool = True,
ignore_lineup: bool = True,
)[source]#

Bases: object

File-like object that duplicates output to multiple file-like objects.

Parameters:
  • *files (IO[Any]) – One or more file-like objects that have write and flush methods

  • strip_colors (bool) – Strip ANSI color codes from output sent to non-stdout/stderr files

  • ascii_only (bool) – Replace non-ASCII characters with their ASCII equivalents for non-stdout/stderr files

  • ignore_lineup (bool) – Ignore lines containing LINE_UP escape sequence in non-terminal outputs

Examples

>>> import sys
>>> f = open("logfile.txt", "w")
>>> sys.stdout = TeeMultiOutput(sys.stdout, f)
>>> print("Hello World")  # Output goes to both console and file
Hello World
>>> f.close()   # TeeMultiOutput will handle any future writes to closed files gracefully
files: tuple[IO[Any], ...]#

File-like objects to write to

strip_colors: bool#

Whether to strip ANSI color codes from output sent to non-stdout/stderr files

ascii_only: bool#

Whether to replace non-ASCII characters with their ASCII equivalents for non-stdout/stderr files

ignore_lineup: bool#

Whether to ignore lines containing LINE_UP escape sequence in non-terminal outputs

property encoding: str#

Get the encoding of the first file, or “utf-8” as fallback.

Returns:

The encoding, ex: “utf-8”, “ascii”, “latin1”, etc.

Return type:

str

write(obj: str) int[source]#

Write the object to all files while stripping colors if needed.

Parameters:

obj (str) – String to write

Returns:

Number of characters written to the first file

Return type:

int

flush() None[source]#

Flush all files.

fileno() int[source]#

Return the file descriptor of the first file.