我正在Mac OS X上的默认python解释器上工作,我Cmd+K(清除)我之前的命令。我可以用方向键逐个浏览它们。但是bash shell中是否存在类似——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))
其他回答
如果你想把历史记录写入一个文件:
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有任何故障,请告诉我。 样品:
完整历史:ipyhistory()
最近10个历史记录:ipyhistory(10)
历史:ipyhistory(-10)
希望它能帮助伙计们。
在IPython中,%history -g应该提供整个命令历史。
默认配置还将历史记录保存到用户目录中名为.python_history的文件中。
使用readline.get_current_history_length()获取长度,并使用readline.get_history_item()查看每个长度。
重新讨论一下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))
推荐文章
- 在Mac OS X上哪里安装Android SDK ?
- 操作系统。makdirs在我的路径上不理解“~”
- 如何在Django模板中获得我的网站的域名?
- 在django Forms中定义css类
- 如何在Python中scp ?
- Mac/OS X上的/var/lib/docker在哪里
- Numpy Max vs amax vs maximum
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 每n行有熊猫
- 实例属性attribute_name定义在__init__之外
- Xcode构建失败“架构x86_64未定义的符号”
- 如何获取在Python中捕获的异常的名称?
- 第一次出现的值大于现有值的Numpy
- 如何从Python函数中返回两个值?
- 前一个月的Python日期