python - 如何在 Python 中存储变量/首选项以备后用

标签 python registry preferences persistent

我正在使用 Python for Windows 开发一个程序,我想保存变量和用户首选项,以便即使在程序终止并重新启动后我也可以调用它们。

有没有在 Windows 机器上执行此操作的理想方法? _winreg 和 Windows 注册表是否适合此任务?还是我需要自己创建某种数据库?

最佳答案

Python2 有 ConfigParser ,这是 configparser,在 Python3 中:

import ConfigParser, os

config = ConfigParser.ConfigParser()
config.readfp(open('defaults.cfg'))
config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')])

即使在 Windows 上,您也应该意识到注册表是一个充斥着败类和邪恶的可悲 hive ,您不应该使用它来存储您的 Python 应用程序配置。

关于python - 如何在 Python 中存储变量/首选项以备后用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10644183/

相关文章:

java - 删除应用程序不同 session 之间的插件首选项

Android 偏好设置 : How to load the default values when the user hasn't used the preferences-screen?

python - 为 QGraphicsPixmapItem 制作动画

windows - 是否有一个 Windows 注册表 "dictionary"可以解释整个(或大部分)Windows 注册表?

python - 两个 DataFrame 中最近的记录以及每条记录之间的对应距离

windows - 根据搜索字符串从 REG QUERY 中提取特定注册表项

registry - 注册表上的防火墙规则

android - 不同进程中的 OnPreferenceChangeListener - 共享偏好数据

python - 使用 Python 查找 javascript 链接

python - 如何创建没有空白行的csv文件