You Can Detect If Code Is Being Run Inside a Terminal

I learned about the sys.stdout.isatty() method in Python which allows you to detect if your code is running in a terminal. For example: import sys if sys . stdout . isatty (): print ( "This program is running in a terminal!" ) else : print ( "This program is running somewhere else, like a CI/CD pipeline" ) Running the program outputs the following: $ python3 tty.py This program is running in a terminal! But if we pipe the program into some other tool, like cat , it is no longer running in a terminal and prints the other message: $ python3 tty.py | cat This program is running somewhere else, like a CI/CD pipeline This is why some colorized output from code is not colorized in CI/CD pipelines. The code itself checks if it’s being run in a terminal, and if not, it doesn’t display colors…

添加评论
点赞收藏
点踩分享查看原文
评论
?
参与讨论