How do I store desktop application data in a cross platform way for python?

后端 未结 4 1705
渐次进展
渐次进展 2020-12-12 16:32

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 usual

4条回答
  •  失恋的感觉
    2020-12-12 16:49

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

提交回复
热议问题