【问题标题】:Save output of chia plots check from console to file将 chia 图检查的输出从控制台保存到文件
【发布时间】:2021-08-17 07:32:12
【问题描述】:
我想将 CLI 命令 chia plots check 的日志记录输出写入文件。
不幸的是,通过chia plots check > file.log 和chia plots check | tee file.log 的常规方式不成功。
编辑python文件也不起作用,因为chia是通过.deb安装的。
感谢您的帮助。
【问题讨论】:
标签:
python
logging
console
stdout
cryptoapi
【解决方案1】:
已解决
我遇到了同样的问题,我找到了答案。
试试这个对我有用
chia plots check &> /home/chia/plot-check.log
注意:如果您想查看实时输出,则必须拖尾文件。
【解决方案2】:
我使用 python 脚本解决了这个问题。
from contextlib import redirect_stdout
import subprocess, re, os
with open('text.txt', 'w') as f:
with redirect_stdout(f):
res = subprocess.check_output('chia plots check', shell=True, stderr=subprocess.STDOUT)
ansi_escape = re.compile(b'(?:\x1B[@-Z\\-_]|[\x80-\x9A\x9C-\x9F]|(?:\x1B\[|\x9B)[0-?]*[ -/]*[@-~])')
res = ansi_escape.sub(b'', res)
print(res.decode('utf-8'))
我已使用 redirect_stdout 将输出流重定向到一个文件(所有打印都定向到一个文件)。使用子进程库来运行 chia。由于输出包含颜色 (ANSI),因此我使用正则表达式来替换输出字节。