Computer >> Computer tutorials >  >> Programming >> Python

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


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!"