<p>试图区分stdout是要进行管道替换还是进程替换</p>
<p>my_python_script.py</p>
<pre><code>#!/usr/bin/python3.6
if sys.__stdin__.isatty():
print("__stdin__ is TTY")
else:
print("__stdin__ is not TTY")
if sys.__stdout__.isatty():
print("__stdout__ is TTY")
else:
print("__stdout__ is not TTY")
if sys.__stderr__.isatty():
print("__stderr__ is TTY")
else:
print("__stderr__ is not TTY")
if sys.stdin.isatty():
print("stdin is TTY")
else:
print("stdin is not TTY")
if sys.stdout.isatty():
print("stdout is TTY")
else:
print("stdout is not TTY")
if sys.stderr.isatty():
print("stderr is TTY")
else:
print("stderr is not TTY")
</code></pre>
<p>如果管道或流程替换,则以下输出相同</p>
<pre><code>> my_python_script.py | cat
__stdin__ is TTY
__stdout__ is not TTY
__stderr__ is TTY
stdin is TTY
stdout is not TTY
stderr is TTY
> cat <(my_python_script.py)
__stdin__ is TTY
__stdout__ is not TTY
__stderr__ is TTY
stdin is TTY
stdout is not TTY
stderr is TTY
</code></pre>
<p>有没有办法区分python脚本中管道的输出和进程替换</p>
<p>多谢各位</p>