如果您喜欢使用交互式会话,则@L_301_1@非常有用。例如,对于您的用例,有一个save命令,您只需输入save my_useful_session 10-20 23即可将输入行10到20和23保存到my_useful_session.py。(为此,每行均以数字作为前缀)
::要么::
# 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=/home/user/.pystartup" in bash.
#
# Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the
# full path to your home directory.
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.parse_and_bind('tab: complete')
请注意,这仅适用于* nix系统。由于readline仅在Unix平台上可用。