Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个小的 Python 实用程序,它只能作为管道运行。我希望它在独立运行时打印出帮助消息。进程如何知道它是否被用作管道。比较sys.stdin并sys.__stdin__不起作用。
sys.stdin
sys.__stdin__
您可以使用isatty:
isatty
if sys.stdin.isatty():
如果True标准输入是一个 tty,这大致意味着它在管道外被直接使用。
True