如何在交互式Python中查看整个命令历史?

我正在Mac OS X上使用默认的python解释器,并且我Cmd+K(清除)了我之前的命令。我可以用方向键逐个浏览它们。但是bash shell中是否存在类似——history选项的选项,可以显示到目前为止输入的所有命令?

150333 次浏览

打印整个历史的代码:

Python 3 .

一行代码(快速复制和粘贴):

import readline; print('\n'.join([str(readline.get_history_item(i + 1)) for i in range(readline.get_current_history_length())]))

(或者更长的版本…)

import readline
for i in range(readline.get_current_history_length()):
print (readline.get_history_item(i + 1))

Python 2 .

一行代码(快速复制和粘贴):

import readline; print '\n'.join([str(readline.get_history_item(i + 1)) for i in range(readline.get_current_history_length())])

(或者更长的版本…)

import readline
for i in range(readline.get_current_history_length()):
print readline.get_history_item(i + 1)

请注意: get_history_item()的索引从1到n。

使用python3解释器,历史记录被写入
~/.python_history < / p >

因为上面只适用于python 2.x 对于python 3。X(特别是3.5)类似,但略有修改:

import readline
for i in range(readline.get_current_history_length()):
print (readline.get_history_item(i + 1))

注意额外的()

(使用shell脚本解析.python_history或使用python修改上面的代码是个人喜好和情况的问题)

这应该会让你在单独的行中打印出命令:

import readline
map(lambda p:print(readline.get_history_item(p)),
map(lambda p:p, range(readline.get_current_history_length()))
)

@Jason-V,真的很有帮助,谢谢。然后,我找到了的例子,并组成了自己的片段。

#!/usr/bin/env python3
import os, readline, atexit
python_history = os.path.join(os.environ['HOME'], '.python_history')
try:
readline.read_history_file(python_history)
readline.parse_and_bind("tab: complete")
readline.set_history_length(5000)
atexit.register(readline.write_history_file, python_history)
except IOError:
pass
del os, python_history, readline, atexit

如果你想把历史记录写入一个文件:

import readline
readline.write_history_file('python_history.txt')

help函数给出:

Help on built-in function write_history_file in module readline:


write_history_file(...)
write_history_file([filename]) -> None
Save a readline history file.
The default filename is ~/.history.

一个类似于unix/bash版本的简单函数来获取历史记录。

希望它能帮助到一些新人。

def ipyhistory(lastn=None):
"""
param: lastn Defaults to None i.e full history. If specified then returns lastn records from history.
Also takes -ve sequence for first n history records.
"""
import readline
assert lastn is None or isinstance(lastn, int), "Only integers are allowed."
hlen = readline.get_current_history_length()
is_neg = lastn is not None and lastn < 0
if not is_neg:
flen = len(str(hlen)) if not lastn else len(str(lastn))
for r in range(1,hlen+1) if not lastn else range(1, hlen+1)[-lastn:]:
print(": ".join([str(r if not lastn else r + lastn - hlen ).rjust(flen), readline.get_history_item(r)]))
else:
flen = len(str(-hlen))
for r in range(1, -lastn + 1):
print(": ".join([str(r).rjust(flen), readline.get_history_item(r)]))

片段:用Python3测试。如果python2有任何故障,请告诉我。 样品:< / p >

完整历史:ipyhistory()

最近10个历史:ipyhistory(10)

历史:ipyhistory(-10)

希望它能帮助伙计们。

在IPython中,%history -g应该给你整个命令历史。默认配置还将历史记录保存到用户目录中名为.python_history的文件中。

重新哈希Doogle的答案,它不打印行号,但允许指定要打印的行数。

def history(lastn=None):
"""
param: lastn Defaults to None i.e full history. If specified then returns lastn records from history.
Also takes -ve sequence for first n history records.
"""
import readline
assert lastn is None or isinstance(lastn, int), "Only integers are allowed."
hlen = readline.get_current_history_length()
is_neg = lastn is not None and lastn < 0
if not is_neg:
for r in range(1,hlen+1) if not lastn else range(1, hlen+1)[-lastn:]:
print(readline.get_history_item(r))
else:
for r in range(1, -lastn + 1):
print(readline.get_history_item(r))