从python2读取带时间戳的shell历史记录

时间:2016-09-14 21:47:47

标签: python shell

我想知道是否可以从python2读取history shell命令的输出。

我试过了:

import subprocess
p = subprocess.Popen("history",shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT )
for line in p.stdout.readlines():
    print line
retval = p.wait()

只是为了测试,但我什么都没有 - 没有输出 - 没有打印。此代码适用于ls

另外我想添加时间戳

HISTTIMEFORMAT="%d/%m/%y %T "

更新 我刚刚意识到history不在/usr/bin/ls

[Jobs:2]  dnl@user ->  whereis history
history: /usr/share/man/man3/history.3.gz /usr/share/man/mann/history.n.gz /usr/share/info/history.info.gz

0 个答案:

没有答案