問題
有沒有辦法告訴交互式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。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
