104
有没有办法检测sys.stdout
是否连接到控制台终端?举例来说,我希望能够检测是否foo.py通过运行:如何检测sys.stdout是否连接到终端?
$ python foo.py # user types this on console
OR
$ python foo.py > output.txt # redirection
$ python foo.py | grep .... # pipe
我之所以问这个问题是我想确保我的进度条显示只发生在前一种情况下(真正的控制台)。
为了证明你的shell,比较`蟒蛇-c “进口SYS;打印(sys.stdout.isatty())”`(应该写TRUE;)与`蟒蛇-c“进口SYS;打印(sys.stdout.isatty())“| grep。(应该写'False')。 – 2013-10-28 19:17:52