亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

問題

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

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

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

解決方案

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

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

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

相關(guān)文章

最新評論