I have a python desktop application that needs to store user data. On Windows, this is usually in %USERPROFILE%\Application Data\AppName\
, on OSX it's usually ~/Library/Application Support/AppName/
, and on other *nixes it's usually ~/.appname/
.
There exists a function in the standard library, os.path.expanduser
that will get me a user's home directory, but I know that on Windows, at least, "Application Data" is localized into the user's language. That might be true for OSX as well.
What is the correct way to get this location?
UPDATE: Some further research indicates that the correct way to get this on OSX is by using the function NSSearchPathDirectory, but that's Cocoa, so it means calling the PyObjC bridge...
Well, for Windows APPDATA (environmental variable) points to a user's "Application Data" folder. Not sure about OSX, though.
The correct way, in my opinion, is to do it on a per-platform basis.
You can try to use
QSettings
from Qt. You can obtain the path to your MyCompany/MyApp.ini file this way:Alternatively, without changing any global state:
On Win7 you get something like:
On Linux (may vary):
I don't know the possible results for OSX (but I'd like to).
QSettings
functionallity seem to be nice until you want to useregisterFormat
, which is not available in PySide, so there is no easy way to use YAML or JSON writers for settings.Well, I hate to have been the one to answer my own question, but no one else seems to know. I'm leaving the answer for posterity.
There's a small module available that does exactly that:
https://pypi.org/project/appdirs/