Sometimes, it can be useful to know if your program is running in a terminal. Since Python 3.3, this functionality has been available in the os module: #!/usr/bin/env python3# Test if this Python script is running in a terminal or not.import os try: size = os.get_terminal_size() print("I am in a terminal of size {}x{}" .format(size[0], size[1])) except OSError: print("I am not in a terminal.") Here is an example of it in operation: