您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

将Python解释器历史记录导出到文件?

将Python解释器历史记录导出到文件?

如果您喜欢使用交互式会话,则@L_301_1@非常有用。例如,对于您的用例,有一个save命令,您只需输入save my_useful_session 10-20 23即可将输入行10到20和23保存到my_useful_session.py。(为此,每行均以数字作为前缀)

查看文档页面上的视频,以快速了解这些功能

::要么::

有一种方法可以做到。将文件存储在?/.pystartup中

# 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平台上可用。

python 2022/1/1 18:29:46 有474人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶