Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store variables/preferences in Python for later use

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?

like image 432
Parker Avatar asked May 17 '12 22:05

Parker


People also ask

How do you store variables in Python?

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.

How do you store a permanent value in a variable in Python?

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.

Can you store a function in a variable Python?

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.


1 Answers

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.

like image 174
Warren P Avatar answered Jan 04 '23 11:01

Warren P