问题:您如何在交互式Python中查看整个命令历史记录?
我正在Mac OS X上使用默认的python解释器,并且Cmd+ K(清除了)我以前的命令。我可以使用箭头键逐一浏览它们。但是bash shell中是否有–history选项之类的选项,可以显示您到目前为止输入的所有命令?
回答 0
回答 1
用于打印整个历史记录的代码:
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。
回答 2
使用python 3解释器将历史记录写入~/.python_history
回答 3
如果要将历史记录写入文件:
import readline
readline.write_history_file('python_history.txt')
帮助功能提供:
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.
回答 4
由于以上内容仅适用于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修改以上代码是个人喜好和情况的问题恕我直言)
回答 5
在IPython中,%history -g
应该为您提供完整的命令历史记录。默认配置还将您的历史记录保存到用户目录中名为.python_history的文件中。
回答 6
一个简单的函数来获取类似于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是否有故障。样品:
完整历史记录:
ipyhistory()
最近的10个历史记录:
ipyhistory(10)
前10个历史记录:
ipyhistory(-10)
希望它能帮助小伙子们。
回答 7
@ 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
回答 8
这应该给您单独打印出的命令:
import readline
map(lambda p:print(readline.get_history_item(p)),
map(lambda p:p, range(readline.get_current_history_length()))
)
回答 9
重排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))
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。