python - 愚弄Python,它是从tty调用的

标签 python shell

这个问题与 Fool python's os.isatty from a bash script 有点相反。我需要 python 脚本来认为它的标准输入已附加到 tty。

特别是,我需要使用一些脚本,它根据 sys.stdin.isatty() 改变其行为。但是当在 bash 循环中调用 python 时,stdin.isatty() 返回 false

cat 'file.txt' | while read ENTRY
do
python -c "import sys; print sys.stdin.isatty()"
done

有没有一种方法可以包装该调用,以便 python 会认为它是从 tty 调用的。

最佳答案

在您的情况下,您可以尝试单独重定向 Python 脚本的输入:

cat 'file.txt' | while read ENTRY
do
  python -c "import sys; print sys.stdin.isatty()" < /dev/tty
done

关于python - 愚弄Python,它是从tty调用的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19766085/

相关文章:

python - 如何将附加数据传递给序列化器?

python - 如何在 Tkinter Python 中创建根据框架调整大小的图像?

Python:一个不会被重新定位的列表

mysql - 自动将每 10 分钟生成的 Web 应用程序日志加载到 mysql

c - 我应该如何理解 "> outfile 2>&1"和 "2>&1 > outfile"?

linux - SSH : how to assign specifics directories to each user?

python - 如果我使用感叹号,为什么 yaml 文件的输出中会出现引号?

python - 这个 Python 2.7 setdefault/defaultdict 代码如何给出嵌套结果?

linux - Bash 脚本第 106 行 : syntax error: unexpected end of file

php - 如果 https 连接,cURL 清空请求数据