best practice for user preferences in $HOME in Python
-
06-09-2019 - |
Question
For some small programs in Python, I would like to set, store and retrieve user preferences in a file in a portable (multi-platform) way.
I am thinking about a very simple ConfigParser file like "~/.program" or "~/.program/program.cfg".
Is os.path.expanduser()
the best way for achieving this or is there something more easy/straightforward?
Solution
os.path.expanduser("~")
is more portable than
os.environ['HOME']
so it should be ok to use the first.
OTHER TIPS
You can use os.environ:
import os
print os.environ["HOME"]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow