[英]Python Write terminal output to file then read file
I'm having trouble writing the terminal output (all print statements) to a textfile then reading that textfile in the same script. 我无法将终端输出(所有打印语句)写入文本文件,然后在同一脚本中读取该文本文件时遇到麻烦。 I keep getting an I/O error if I close the program to finish writing to the file and then re-open the file to read it, or no output for the final print(file_contents) statement. 如果我关闭程序以完成对文件的写入,然后重新打开文件以读取文件,或者最后的print(file_contents)语句无输出,则始终收到I / O错误。
Here's my code: 这是我的代码:
import sys
filename = open("/Users/xxx/documents/python/dump.txt", 'r+')
filename.truncate()
sys.stdout = filename
print('Hello')
print('Testing')
filename.close()
with open("/Users/xxx/documents/python/dump.txt") as file:
data = file.read()
print(file)
Any suggestions would be great! 任何建议都很好! I'm planning to use this to print output's from some longer scripts to a slack channel. 我打算使用它来将输出从一些较长的脚本打印到一个松弛的通道。
Thanks! 谢谢!
The error you get is: 您得到的错误是:
IOError: [Errno 2] No such file or directory: '/Users/xxx/documents/python/dump.txt'
because: file open mode r+
does not create a file. IOError: [Errno 2] No such file or directory: '/Users/xxx/documents/python/dump.txt'
因为:文件打开模式r+
不会创建文件。 Use mode w
like this: 像这样使用模式w
:
You have to reattach stdout
to console again to print in console. 您必须重新将stdout
附加到控制台才能在控制台中打印。
import sys
filename = open('/Users/xxx/documents/python/dump.txt', 'w')
# filename.truncate() # mode 'w' truncates file
sys.stdout = filename
print('Hello')
print('Testing')
filename.close()
# reattach stdout to console
sys.stdout = sys.__stdout__
with open('/Users/xxx/documents/python/dump.txt') as file:
data = file.read()
print(data)
will print: 将打印:
Hello
Testing
The problem is you redirect sys.stdout
to filename
, and then you close the file. 问题是您将sys.stdout
重定向到filename
,然后关闭该文件。 Afterwards you can't print anything anymore, since the file is closed. 此后,由于文件已关闭,您无法再打印任何内容。
sys.stdout = filename
..
..
filename.close()
with open("/Users/xxx/documents/python/dump.txt") as file:
data = file.read()
print(file)
The last print statement tries to print output to sys.stdout , which is a closed file. 最后一个打印语句尝试将输出打印到sys.stdout ,这是一个关闭的文件。
If you want to get the old behavior back, you need to keep a reference to sys.stdout
. 如果要恢复旧的行为,则需要保留对sys.stdout
的引用。 This will solve it: 这将解决它:
sys_out = sys.stdout
sys.stdout = filename
..
..
filename.close()
sys.stdout = sys_out
with open("/Users/xxx/documents/python/dump.txt") as file:
data = file.read()
print(file)
import sys 导入系统
filename = open("/Users/xxx/documents/python/dump.txt", 'w') filename = open(“ / Users / xxx / documents / python / dump.txt”,'w')
sys_out = sys.stdout sys.stdout = filename sys_out = sys.stdout sys.stdout =文件名
print('Hello') 打印('Hello')
print('Testing') 打印('测试')
print('Test') 打印('测试')
filename.close() filename.close()
sys.stdout = sys_out sys.stdout = sys_out
with open("/Users/xxx/documents/python/dump.txt", 'r') as file: data = file.read() print(data) 使用open(“ / Users / xxx / documents / python / dump.txt”,'r')作为文件:data = file.read()print(data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.