【发布时间】:2012-12-05 18:42:49
【问题描述】:
我正在使用 python 来运行一些 shell 脚本、RScripts、python 程序等。这些程序可能会运行很长时间,并且可能会向 stdout 和 stderr 输出大量(日志记录)信息。我正在使用以下(Python 2.6)代码,它工作正常:
stdoutFile=open('stdout.txt', 'a')
stderrFile=open('stderr.txt', 'a')
subprocess.call(SHELL_COMMAND, shell=True, stdout=stdoutFile, stderr=stderrFile)
stdoutFile.close()
stderrFile.close()
这主要是记录到文件中的信息,这些信息可以在很长一段时间内生成。因此我想知道是否可以在每一行前面加上日期和时间?
例如,如果我当前要登录:
Started
Part A done
Part B done
Finished
那么我希望它是:
[2012-12-18 10:44:23] Started
[2012-12-18 12:26:23] Part A done
[2012-12-18 14:01:56] Part B done
[2012-12-18 22:59:01] Finished
注意:修改我运行的程序不是和选项,因为这个 python 代码有点像这些程序的包装器。
【问题讨论】:
标签: python datetime logging subprocess stdout