Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何禁用python历史记录保存?_Python 3.x - Fatal编程技术网

Python 3.x 如何禁用python历史记录保存?

Python 3.x 如何禁用python历史记录保存?,python-3.x,Python 3.x,我在linux(OpenSuse)机器上使用CPython和 22;~/.../XO/> python3 --version Python 3.6.10 我想为启动会话的每个工作目录分别设置保存/加载python交互式shell历史记录。 我正试图在/etc/pythonstart中实现这一点,并将导出pythonstart=/etc/pythonstart 我的/etc/pythonstart的相关内容是 import os import readline import atexit

我在linux(OpenSuse)机器上使用CPython和

22;~/.../XO/> python3 --version
Python 3.6.10
我想为启动会话的每个工作目录分别设置保存/加载python交互式shell历史记录。 我正试图在
/etc/pythonstart
中实现这一点,并将
导出pythonstart=/etc/pythonstart

我的
/etc/pythonstart
的相关内容是

import os
import readline
import atexit

historyFile = ".pyhistory"
cwd         = os.getcwd()

historyPath = cwd + "/" + historyFile

def save_history():
    try:
        readline.write_history_file(historyPath)
    except:
        pass

# read history
if os.path.exists(historyPath):
    readline.set_history_length(100)
    readline.read_history_file(historyPath)

# save history
atexit.register(save_history)

它几乎可以工作,除了python还可以在
~/.python\u history
中保存和读取历史记录,并且可以合并所有会话。有没有办法禁用后面的行为?如果有,是如何禁用的?

对启动文件的这种修改会起作用。它不会禁用写入
~/.python\u history
,但会在启动时忽略其内容

import atexit
import os
import readline

historyFile = ".pyhistory"
startFile   = ".pystart"
cwd         = os.getcwd()

historyPath = cwd + "/" + historyFile
startPath   = cwd + "/" + startFile

def save_history(historyPath=historyPath):
    import readline
    try:
        readline.write_history_file(historyPath)
        readline.clear_history()
    except:
        pass

# ignore history loaded from the standard file
readline.clear_history()

# load history from the custom file
if os.path.exists(historyPath):
    readline.set_history_length(100)
    readline.read_history_file(historyPath)

# register handler
atexit.register(save_history)

我的zshrc中有以下内容:

export PYTHONSTARTUP=~/.config/python/pythonrc
pythonrc
文件中:

import readline
readline.write_history_file = lambda *args: None
它直接禁止写入历史文件,而不是完全禁用历史


(我正在使用python 3.8.5)

谢谢,但完全禁用历史记录并不是目的。我想分别保存每个cwd的历史记录。可以使用
readline进行禁用。设置历史记录长度(0)
。我自己的答案中的技巧是有效的,尽管它太粗糙了。