How to check if a file is connected with terminal in Python?

PythonServer Side ProgrammingProgramming

You can check if your current script is connected with the terminal or not using the isatty() function. For example,

import sys
if sys.stdout.isatty():
    print("Inside a terminal!")
else:
    print("Piped output")

If you run the above from a terminal, you'll get the output:

"Inside a terminal!"
raja
Published on 22-Dec-2017 11:09:50
Advertisements