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?
In some programming languages you have to declare a variable before using them or define the information that will be stored in it, e.g., a number. However, in Python we just need to type the name of our variable, followed by an equals sign and a value to assign to it.
For example: in the command prompt of Windows, we have stuff like %USERNAME%. This is a permanent thing, but you can enter regedit and set a new permanent stuff like %PYTHON% and it will do whatever you write there.
In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want. Thereby, increasing code reusability. Simply assign a function to the desired variable but without () i.e. just with the name of the function.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With