您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

给Python终端一个持久的历史

给Python终端一个持久的历史

当然可以,只需一个小的启动脚本。从交互式输入编辑及历史替换在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开始,交互式解释器开箱即用地支持自动完成和历史记录

现在,在支持的系统上的交互式解释器中,认情况下启用了制表符完成功能readline认情况下,历史记录也处于启用状态,并且被写入文件(或从文件中读取)~/.python- history

python 2022/1/1 18:13:50 有575人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶