python - 如何将 sys.stdout 重新附加到 python 中的控制台窗口?

标签 python io stdout stringio redirectstandardoutput

我的 python 3 涂鸦是这样的:

import io, sys
sys.stdout = io.StringIO()
# no more responses to python terminal funnily enough

我的问题是如何重新附加,以便当我传入 1+1 时,它会返回 2 到控制台?

这是在 windows 7 64 位上运行的 32 位 python 的 python 解释器中。

最佳答案

您正在寻找 sys.__stdout__ :

It can also be used to restore the actual files to known working file objects in case they have been overwritten with a broken object. However, the preferred way to do this is to explicitly save the previous stream before replacing it, and restore the saved object.

关于python - 如何将 sys.stdout 重新附加到 python 中的控制台窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30925413/

相关文章:

java - 将输入保存到文件

c - 使用系统函数 read() 和 write() 时 Linux 在终端中输出的机制

python - 改变单个词的情绪

python - 如何在粗线上获得非圆形箭头?

python - 将字符串中的每个字符转换为字典键

python - 为什么 shell 输出会转到 stderr?

c - stdout 必须是文本流吗?

python - 每次使用 Python 运行时都会生成新的文本文件

c++ - getline 函数的多个分隔符,c++

c - 在 ansi c 中读取和格式化文本文件