How do I store desktop application data in a cross

2020-02-07 16:01发布

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...

4条回答
疯言疯语
2楼-- · 2020-02-07 16:23

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.

查看更多
爷的心禁止访问
3楼-- · 2020-02-07 16:24

You can try to use QSettings from Qt. You can obtain the path to your MyCompany/MyApp.ini file this way:

from PySide.QtCore import QSettings, QCoreApplication

QSettings.setDefaultFormat(QSettings.IniFormat)
QCoreApplication.setOrganizationName("MyCompany")
QCoreApplication.setApplicationName("MyApp")
settings = QSettings()
print(settings.fileName())

Alternatively, without changing any global state:

QSettings(
    QSettings.IniFormat, QSettings.UserScope,
    "MyCompany", "MyApp"
).fileName()

On Win7 you get something like:

C:\Users\MyUser\AppData\Roaming\MyCompany\MyApp.ini

On Linux (may vary):

/home/myuser/.config/MyCompany/MyApp.ini

I don't know the possible results for OSX (but I'd like to).

QSettings functionallity seem to be nice until you want to use registerFormat, which is not available in PySide, so there is no easy way to use YAML or JSON writers for settings.

查看更多
做个烂人
4楼-- · 2020-02-07 16:36

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.

APPNAME = "MyApp"
import sys
from os import path, environ
if sys.platform == 'darwin':
    from AppKit import NSSearchPathForDirectoriesInDomains
    # http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSSearchPathForDirectoriesInDomains
    # NSApplicationSupportDirectory = 14
    # NSUserDomainMask = 1
    # True for expanding the tilde into a fully qualified path
    appdata = path.join(NSSearchPathForDirectoriesInDomains(14, 1, True)[0], APPNAME)
elif sys.platform == 'win32':
    appdata = path.join(environ['APPDATA'], APPNAME)
else:
    appdata = path.expanduser(path.join("~", "." + APPNAME))
查看更多
Bombasti
5楼-- · 2020-02-07 16:36

There's a small module available that does exactly that:

https://pypi.org/project/appdirs/

查看更多
登录 后发表回答