python - 临时更改 sys.stdout 会给出“ValueError: I/O operation on closed file”
问题描述
来自 StackOverflow 的大家好。我正在做 Marco Buttu 的“Programmare con Python: Guida completa”中的示例。
目的是临时更改 sys.stdout 以逃避将参数传递给 print()
>>> import sys
>>> sys.stdout = open('myfile.log', 'w')
>>> print('escribo en el archivo log...')
>>> print('todavia en el archivo log...')
>>> sys.stdout.close()
>>> open('myfile.log').read()
我收到了这个错误。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file.
有人可以向我解释为什么会这样吗?
是否因版本不同而出现错误(书本版本:3.4,实际版本:3.6.5)
我会寻找你的答案。
非常感谢你。
解决方案
推荐阅读
- php - 展平嵌套关联数组,同时保留键值组
- powershell - Powershell Microsoft Teams 错误,代码未找到,尽管似乎没有进行任何更改
- javascript - JS:同时使用过滤和映射的替代方案?
- javascript - 如果在浏览器中存储访问令牌不安全,那么在浏览器中使用 octokit/core.js 有什么意义?
- postgresql-10 - 如何在 PostgreSQL10 中启用 ssl
- python - 有没有更快的方法来提取列表中的元素?
- assembly - 如何阅读这段 Rust 代码的汇编代码?
- javascript - React.js:在更改状态的输入字段中单击取消时如何返回先前的状态值
- javascript - 无法将值解析到 Vue2Editor
- docker - zap-api-scan.py 无法扫描 JSON 文件中的 api 路径