Python output to Console within Subprocess from the child scricpt

If stdout, stderr are redirected then you could try to print directly to the console:

try: # Windows
    from msvcrt import putwch

    def print_to_console(message):
        for c in message:
            putwch(c)
        # newline
        putwch('\r') 
        putwch('\n')
except ImportError: # Unix
    import os

    fd = os.open('/dev/tty', os.O_WRONLY | os.O_NOCTTY)
    tty = os.fdopen(fd, 'w', 1)
    del fd
    def print_to_console(message, *, _file=tty):
        print(message, file=_file)
    del tty

Example:

print_to_console("Hello TTY!")
# -> Hello TTY!

Leave a Comment