import os import atexit import readline history = os.path.join(os.environ['XDG_CACHE_HOME'], 'python_history') readline.read_history_file(history) def write_history(): readline.write_history_file(history) atexit.register(write_history)