我正在Mac OS X上使用默认的python解释器,并且Cmd+ K(清除了)我以前的命令。我可以使用箭头键逐一浏览它们。但是bash shell中是否有--history选项之类的选项,可以显示您到目前为止输入的所有命令?
history
是内置的shell。
我正在Mac OS X上使用默认的python解释器,并且Cmd+ K(清除了)我以前的命令。我可以使用箭头键逐一浏览它们。但是bash shell中是否有--history选项之类的选项,可以显示您到目前为止输入的所有命令?
history
是内置的shell。
Answers:
用于打印整个历史记录的代码:
单线(快速复制和粘贴):
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))
单线(快速复制和粘贴):
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。
import readline; print '\n'.join([str(readline.get_history_item(i)) for i in range(readline.get_current_history_length())])
history()
在Python解释器启动脚本(由env。var指向的脚本)中添加了上述函数$PYTHONSTARTUP
。从现在开始,我只需键入history()
任何解释程序会话即可;-)
使用python 3解释器将历史记录写入
~/.python_history
cat ~/.python_history
quit()
口译员才能将当前会话历史记录包括在内~/.python_history
如果要将历史记录写入文件:
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.
由于以上内容仅适用于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修改以上代码是个人喜好和情况的问题恕我直言)
C:\>python -m pip install readline
=> Collecting readline
\ n Downloading https://files.pythonhosted.org/packages/f4/01/2cf081af8d880b44939a5f1b446551a7f8d59eae414277fd0c303757ff1b/readline-6.2.4.1.tar.gz (2.3MB)
\ n |████████████████████████████████| 2.3MB 1.7MB/s
\ n ERROR: Complete output from command python setup.py egg_info:
\ n ERROR: error: this module is not meant to work on Windows
\ n ----------------------------------------
\ n`ERROR:命令“ python setup.py egg_info”在C:\ Users \ dblack \ AppData \ Local \ Temp \ pip-install-s6m4zkdw中失败,错误代码为1 \ readline`
pip install readline
,但是readline
默认情况下已在Windows上安装。
一个简单的函数来获取类似于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)
希望它能帮助小伙子们。
cut -c 8
把它们淘汰。)
@ 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
map(lambda p:print(readline.get_history_item(p)),
map(lambda p:p, range(readline.get_current_history_length()))
)
重排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))
history
外壳命令是像任何其他的程序。它不是bash
命令中的“选项” 。