开发者

Check whether the process is being run as a pipe

开发者 https://www.devze.com 2023-01-26 10:39 出处:网络
I have a small Python utility which should be run only as a pipe. I want it to print out the help message when it runs stand alone. How can a process know whether it is being used as a p开发者_Python百

I have a small Python utility which should be run only as a pipe. I want it to print out the help message when it runs stand alone. How can a process know whether it is being used as a p开发者_Python百科ipe. Comparing sys.stdin and sys.__stdin__ does not work.


You can use isatty:

if sys.stdin.isatty():

It will be True if standard input is a tty, which roughly means it's being used directly, outside a pipe.

0

精彩评论

暂无评论...
验证码 换一张
取 消