如何为Python终端提供持久性历史记录
2019-09-03 14:38
1561 查看
问题
有没有办法告诉交互式Python shell在会话之间保留其执行命令的历史记录?
当会话正在运行时,在执行命令之后,我可以向上箭头并访问所述命令,我只是想知道是否有某种方法可以保存这些命令,直到下次我使用Python shell时。
这非常有用,因为我发现自己在会话中重用命令,这是我在上一个会话结束时使用的。
解决方案
当然你可以用一个小的启动脚本。来自python教程中的交互式输入编辑和历史替换:
# Add auto-completion and a stored history file of commands to your Python # interactive interpreter. Requires Python 2.0+, readline. Autocomplete is # bound to the Esc key by default (you can change it - see readline docs). # # Store the file in ~/.pystartup, and set an environment variable to point # to it: "export PYTHONSTARTUP=~/.pystartup" in bash. import atexit import os import readline import rlcompleter historyPath = os.path.expanduser("~/.pyhistory") def save_history(historyPath=historyPath): import readline readline.write_history_file(historyPath) if os.path.exists(historyPath): readline.read_history_file(historyPath) atexit.register(save_history) del os, atexit, readline, rlcompleter, save_history, historyPath
从Python 3.4开始,交互式解释器支持开箱即用的自动完成和历史记录:
现在,在支持的系统上的交互式解释器中默认启用Tab-completion readline。默认情况下也会启用历史记录,并将其写入(并从中读取)文件~/.python-history。
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- 如何让python idle有命令自动补全和查看历史记录的方法
- python-5-如何实现用户的历史记录功能(最多n条)?
- 如何使linux系统下的root用户不保存终端历史记录
- python 2-7 如何实现用户的历史记录功能(最多n条)collections.deque/pickle持久存储
- 如何使linux系统下的root用户不保存终端历史记录到.bash_history中
- Python高级编程-如何实现用户的历史记录功能?
- 如何查看移动平均价的历史记录?
- Git如何永久删除文件(包括历史记录)
- 如何手动屏蔽Win7资源管理器的历史记录 保护隐私安全
- textbox html标签 如何去掉历史记录
- 如何获取url访问历史记录
- plsql developer如何查询SQL语句执行历史记录
- 遨游历史记录如何调出来?
- Python如何记录日志
- 如何在WinDBG里保存运行过的命令的历史记录?
- Ajax请求如何实现页面无刷新前进后退,将ajax请求保存至浏览器历史记录
- 如何消除TextBox框中显示的历史记录?
- 如何将ipython的历史记录导出到.py文件中?
- 如何查询SQL Server备份还原历史记录
- Git如何删除历史记录中的大文件详解