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

后端 未结 4 1711
渐次进展
渐次进展 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 17:15

    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.

提交回复
热议问题