python终端会话_给Python终端一个持久的历史

该博客介绍了如何在Python交互式解释器中设置自动补全和历史记录功能。通过一个启动脚本,可以利用readline模块实现制表符补全,并将历史命令保存到~/.python-history文件中。这个功能对于日常Python开发和学习非常有帮助。
摘要由CSDN通过智能技术生成

小编典典

当然可以,只需一个小的启动脚本。从交互式输入编辑及历史替换在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

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

history。

2020-06-02

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值