如果将stdout,stderr重定向,则可以尝试直接打印到控制台:
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例:
print_to_console("Hello TTY!")# -> Hello TTY!


