【发布时间】:2011-04-18 15:00:14
【问题描述】:
我在 Linux 中工作,想知道如何让 python 判断它是直接从终端运行还是通过 GUI(如 alt-F2)运行,其中输出需要发送到窗口而不是 stdout将出现在终端中。
在 bash 中,这由以下人员完成:
if [ -t 0 ] ; then
echo "I'm in a terminal"
else
zenity --info --title "Hello" --text "I'm being run without a terminal"
fi
这如何在 python 中完成?换句话说,相当于[ -t 0 ])?
【问题讨论】:
-
How can I check to see if a Python script was started interactively? 的可能重复简单方法:
sys.stdout.isatty() -
请注意
isatty将无法正确检测管道 -
sys.stdout.isatty() 和 os.isatty(0) 正是我想要的。伟大的。谢谢。如果您将其作为答案,我会接受。
-
@cobbal,你是对的。这是概念的复制品,只是以不同的方式提出。谢谢。
标签: python user-interface shell testing