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 – One or more file-like objects that have write and flush methods

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

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

  • ignore_lineup – 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], ...][source]#

File-like objects to write to

strip_colors: bool[source]#

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

ascii_only: bool[source]#

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

ignore_lineup: bool[source]#

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

property encoding: str[source]#

Get the encoding of the first file, or “utf-8” as fallback. :returns: “utf-8”, “ascii”, “latin1”, etc. :rtype: The encoding, ex

write(obj: str) int[source]#

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

Parameters:

obj – String to write

Returns:

Number of characters written to the first file

fileno() int[source]#

Return the file descriptor of the first file.

isatty() bool[source]#

Return True if the first file is a terminal/console.