How to store variables/preferences in Python for later use

混江龙づ霸主 提交于 2020-01-02 02:00:29

问题


I'm working on a program in Python for Windows, and would like to save variables and user preferences so that I can recall them even after the program has been terminated and restarted.

Is there an ideal way to do this on Windows machines? Would _winreg and the Windows registry be suited for this task? Or do I need to create some sort of database of my own?


回答1:


You're usually going to want to store it in a configuration folder in the "home" folder. That's easy on *nix systems, more difficult in windows, because you actually need to get the "application data" directory. This usually works for me:

import os
if os.name != "posix":
    from win32com.shell import shellcon, shell
    homedir = "{}\\".format(shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0))
else:
    homedir = "{}/".format(os.path.expanduser("~"))

After you have the homedirectory, you should create a folder named after your project:

if not os.path.isdir("{0}.{1}".format(homedir,projectname)):
    os.mkdir("{0}.{1}".format(homedir,projectname))

Then you can make a config file in that folder and write your options to it in the format of your choosing (my personal favorite is in an XML).




回答2:


Python2 has ConfigParser, which is configparser, in Python3:

import ConfigParser, os

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

Even on windows, you should be aware that the registry is a wretched hive of scum and villainy, and that you should not be using it to store your python app configurations.




回答3:


Unless I"m missing something, regex has nothing to do with the windows registry. Just store a config file somewhere, like the user's home directory or whatever. At least that's what I'd do on linux. Store a file somewhere in ~/.config/




回答4:


If you want to store just key-value pairs of preferences, do it in a text config file. If you want to store python structures, do it with pickle or shelve. If you need a light, hassle free, rdbms use sqlite.




回答5:


While the pickle module is an obvious choice, the files it writes will be non-human-readable. For configuration files, it would be good if your configuration file was a simple text format that your users can read or even edit. So, I recommend you use JSON and the json module.




回答6:


There are several ways, devided into two main directions

  • Access the Windows registry with the _winreg module.

Using the registry it is nontrivial to transfer the settings to another machine or to inspect them or back them up; you'd have to use regedit for those.

The other way store the preferences in a file in the user's 'My Documents' folder. This makes it easier to move the settings to another machine or user, and doesn't mix your program's settings with those of a host of other applications. So things like backing them up and restoring them are easier; you just copy one file. If you choose a text format, it is also easier to inspect and debug the settings.

  • Put your settings in a list, tuple or dictionary and save them with the cPickle module. This is probably one of the easiest methods. On the downside, while pickled data uses an ASCII format by default, it is not human-readable.
  • Use the ConfigParser module to save and load config files in a similar structure as Windows' .ini files. These files are human-readable.
  • Use the json module to store settings in json format. These files are human-readable.
  • Save the preferences as Python expressions to a text file, load it with execfile(). N.B. This could be used to execute arbitrary code, so there are safety considerations. But on the upside, it is very easy and readable.



回答7:


To save variables, I recommend using pickle



来源:https://stackoverflow.com/questions/10644183/how-to-store-variables-preferences-in-python-for-later-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!