【问题标题】:Python output redirectingPython 输出重定向
【发布时间】:2018-09-10 12:51:30
【问题描述】:

当我在 Linux 机器上运行 python3 解释器并尝试将它的 stdout 重定向到这样的文件时,没有任何反应:

user@workmachine:~$ python3 > python.txt
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
\>>>

文件python.txt 在解释器运行时以及关闭后保持为空。它的所有输出仍在终端中。

另一方面,如果我重定向 R 解释器 以同样的方式(R > R.txt)结果符合预期(所有输出重定向到文件,在终端中看不到任何内容)。

有什么区别? python 是否将其所有输出写入stdout 之外的另一个流,还是什么?

【问题讨论】:

标签: python linux io


【解决方案1】:

对于您的情况,python 似乎将信息发送到 STDERR 而不是 STDOUT。所以你应该像这样使用重定向:

user@workmachine:~$ python3 2> python.txt

【讨论】:

  • 交互式程序写入标准错误而不是标准输出是很常见的。例如,read -p "Type something: " 将在 stderr 上显示其提示。
猜你喜欢
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多