import sys if sys.stdin.isatty(): print("Input is coming from terminal") else: print("Input is coming from a file or pipe")
import os if os.isatty(1): print("Output is going to the terminal") else: print("Output is going to a file or pipe")In this example, we are checking whether the output stream is going to the terminal or not. We are using the isatty function of the os module for this purpose. If it returns True for file descriptor 1 (which represents the standard output stream), then we print that the output is going to the terminal, otherwise, we assume that it is going to a file or pipe. Package/library: Python typing IO isatty attribute is part of the Python built-in I/O module, which is included in the standard library. Therefore, it does not require any external package/library to be installed.