如何以跨平台方式存储Python桌面应用程序数据?

发布于 2024-07-26 02:31:12 字数 471 浏览 5 评论 0原文

我有一个 python 桌面应用程序需要存储用户数据。 在 Windows 上,这通常位于 %USERPROFILE%\Application Data\AppName\ 中,在 OSX 上,通常位于 ~/Library/Application Support/AppName/ 中,在其他 *nix 上通常是~/.appname/

标准库中存在一个函数 os.path.expanduser 可以让我获得用户的主目录,但我知道至少在 Windows 上,“应用程序数据”已本地化为用户的语言。 对于 OSX 来说可能也是如此。

获取此位置的正确方法是什么?

更新: 一些进一步的研究表明,在 OSX 上获取此信息的正确方法是使用函数 NSSearchPathDirectory,但那是 Cocoa,所以这意味着调用 PyObjC 桥...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花开浅夏 2024-08-02 02:31:12

好吧,我讨厌自己回答自己的问题,但似乎没有人知道。 我把答案留给后人。

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

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))
〃安静 2024-08-02 02:31:12

有一个小模块可以做到这一点:

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

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

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

じее 2024-08-02 02:31:12

您可以尝试使用 Qt 中的 QSettings 。 您可以通过以下方式获取 MyCompany/MyApp.ini 文件的路径:

from PySide.QtCore import QSettings, QCoreApplication

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

或者,在不更改任何全局状态的情况下:

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

在 Win7 上,您会得到类似以下内容:

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

在 Linux 上(可能有所不同):

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

我不知道 OSX 的可能结果(但是我想)。

QSettings 功能似乎很不错,直到您想使用 registerFormat 为止,PySide 中不提供该功能,因此没有简单的方法可以使用 YAML 或 JSON 编写器进行设置。

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.

不必了 2024-08-02 02:31:12

那么,对于 Windows APPDATA(环境变量)指向用户的“应用程序数据”文件夹。 但不确定 OSX 是否如此。

在我看来,正确的方法是针对每个平台进行操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文