欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何為Python終端提供持久性歷史記錄

 更新時(shí)間:2019年09月03日 14:38:38   作者:python之蟬  
這篇文章主要介紹了如何為Python終端提供持久性歷史記錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

問(wèn)題

有沒(méi)有辦法告訴交互式Python shell在會(huì)話之間保留其執(zhí)行命令的歷史記錄?

當(dāng)會(huì)話正在運(yùn)行時(shí),在執(zhí)行命令之后,我可以向上箭頭并訪問(wèn)所述命令,我只是想知道是否有某種方法可以保存這些命令,直到下次我使用Python shell時(shí)。

這非常有用,因?yàn)槲野l(fā)現(xiàn)自己在會(huì)話中重用命令,這是我在上一個(gè)會(huì)話結(jié)束時(shí)使用的。

解決方案

當(dāng)然你可以用一個(gè)小的啟動(dòng)腳本。來(lái)自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開(kāi)始,交互式解釋器支持開(kāi)箱即用的自動(dòng)完成和歷史記錄

現(xiàn)在,在支持的系統(tǒng)上的交互式解釋器中默認(rèn)啟用Tab-completion readline。默認(rèn)情況下也會(huì)啟用歷史記錄,并將其寫入(并從中讀取)文件~/.python-history。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論