跨平台桌面目录路径? [英] Cross-platform Desktop directory path?

查看:241
本文介绍了跨平台桌面目录路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过Python跨平台的方式获得桌面目录路径,理想情况下只使用Python中的标准模块?



我目前的Mac OS X + Windows解决方案是使用 sys.platform ,然后执行以下操作:





我会很高兴一个适用于Mac OS X,Windows和Linux的解决方案。

解决方案

在windows下面,用户主页是%HOMEPATH%这相当于linux和Mac 。在这下面,就像在Mac上有一个文件夹 Desktop Python会在Windows上自动将转换为%HOMEPATH%,因此您的Mac命令将在Mac和Windows上开箱即用。


$ b $在Linux上,这有点棘手。首先,了解您正在运行的Linux框可能不具有一个桌面,因此没有用户桌面文件夹。如果你有窗口管理器,它可能会也可能不会遵循〜\Desktop 范例。
窗口管理员的维基百科条目更加详细,包括几个更受欢迎的比较X窗口管理器中的某些子链接。



你最好的选择是退后一步,问自己为什么我需要/需要用户的桌面文件夹?是创建一个安装过程中的快捷方式?您可能会更好地使用安装编写器实用程序,例如 nsis ,处理这些详细信息。如果它是文件存储,即使是临时的,你可能想重新思考你的设计。或者你正在寻找东西,在这种情况下,文件系统搜索可能是要走的路,而不是一个脆弱的单个文件夹检查。



像大多数事情一样,这一切取决于你想要完成的任务。



正如EOL在评论中指出的那样,Windows比初次出现时稍微棘手一点。 他链接到Windows上更完整的文章Desktop 文件夹具有有关桌面文件夹的本地化的更多详细信息。这对于国际应用程序的构建者来说非常重要,要考虑到使用自己的工具集中的自动本地化,或避免使用它们的东西。


Is there a way of obtaining the Desktop directory path in a cross-platform way, ideally only using standard modules, in Python?

My current Mac OS X + Windows solution is to check which system is running Python with sys.platform and then do the following:

  • Mac OS X can be handled with os.path.join(os.path.expanduser('~'), 'Desktop').
  • Windows can use the non-standard module win32com, or the ctypes-dependent module winpaths; is there a standard alternative?
  • And what about Linux?

I would be happy with a solution that works on Mac OS X, Windows and Linux.

解决方案

Underneath windows, the users home is %HOMEPATH% which is the equivalent of the linux and Mac ~. Underneath this, there is a folder Desktop just like on Mac. Python automatically converts ~ to %HOMEPATH% on windows, so your Mac command will work out of the box on Mac and Windows.

On Linux, it's a bit trickier. First, understand that the Linux box you are running on may not have a desktop, so no user desktop folder. If you do have window manager, it may or may not follow the ~\Desktop paradigm. The wikipedia entry on window managers goes into far more detail, including comparisons between several of the more popular X window managers in some of the sublinks.

Your best bet would be to step back, and ask yourself why do I want/need the users desktop folder? Is it to create a shortcut during the install? You are likely better off with a installation writer utility, such as nsis, handling those details. If it's for file storage, even temporary, you may want to rethink your design. Or are you looking for something, in which case a file system search may be the way to go, instead of a brittle single folder check.

Like most things, it all depends on what you want to accomplish.

As EOL noted in his comment, Windows is slightly trickier than it first appears. His link to a more complete article on the Windows Desktop folder has more details on localization for the desktop folder. This is very important for builders of international applications to take into account, either using automatic localization built into their toolset, or avoiding things that use it.

这篇关于跨平台桌面目录路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆