【发布时间】:2021-01-06 23:33:36
【问题描述】:
我有一个运行 python 文件的脚本。该脚本在执行操作时不断将 python 的打印语句重定向到文件。
#!bin/bash
. venv/bin/activate
python3 mypythonfile.py > logfile.txt
当我跑步时
cat logfile.txt
刷新其内容似乎总是需要大约 30 秒到一分钟;换句话说,我无法看到正在添加到 logfile.txt 中的信息。
有什么建议吗?
我记得几年前在网上看到过类似命令的东西,但是结果和论坛通常会提到如何查找正在编写的文件,而这些文件不是我要搜索的:/
任何帮助和指点将不胜感激;)
编辑:由于某种原因,此问题被标记为与以下线程相关:
Python: subprocess.call, stdout to file, stderr to file, display stderr on screen in real time
虽然这对其他项目肯定有帮助,但它根本不能像这里给出的答案一样受到重视。
谢谢大家的cmets。
【问题讨论】:
-
这个问题不属于 Stack Overflow。它更适合Super User。无论如何,您要查找的命令是
tail -f。 -
tail -f /path/thefile.log将为您提供正在写入的日志的“实时”视图,请参见例如howtogeek.com/howto/ubuntu/a-live-view-of-a-logfile-on-linux -
用
python3 -u ...尝试无缓冲模式。 -
在你的python脚本中,你可能需要定期
sys.stdout.flush()。 -
或使用
flush=Truearg 到print