跨平台桌面目录路径?
有没有一种方法可以在Python中以跨平台的方式获取桌面目录路径,最好只使用标准模块?
我当前的 Mac OS X + Windows 解决方案是使用 sys 检查哪个系统正在运行 Python。 platform,然后执行以下操作:
- Mac OS X 可以使用 os.path.join(os.path.expanduser('~'), 'Desktop') 进行处理。
- Windows 可以使用非标准模块
win32com
,或依赖于 ctypes 的模块 winpaths;有标准的替代方案吗? - 那么 Linux 呢?
我很高兴能找到一个适用于 Mac OS X、Windows 和 Linux 的解决方案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用了以下内容:
参考:os.path.expanduser
I used the following:
Reference: os.path.expanduser
在windows下,用户的home是
%HOMEPATH%
,相当于linux和Mac的~
。在其下方,有一个文件夹Desktop
,就像在 Mac 上一样。 Python 在 Windows 上自动将~
转换为 %HOMEPATH%,因此您的 Mac 命令将在 Mac 和 Windows 上开箱即用。在 Linux 上,这有点棘手。首先,请了解您运行的 Linux 机器可能没有桌面,因此没有用户桌面文件夹。如果您有窗口管理器,它可能遵循也可能不遵循
~\Desktop
范例。 有关窗口管理器的维基百科条目提供了更多详细信息,包括几种更流行的窗口管理器之间的比较X 窗口管理器中的一些子链接。您最好的选择是退后一步,问自己为什么我想要/需要用户桌面文件夹?是为了在安装过程中创建快捷方式吗?您可能最好使用安装编写器实用程序,例如 nsis 来处理这些详细信息。如果它用于文件存储,即使是临时的,您可能需要重新考虑您的设计。或者您正在寻找某些东西,在这种情况下,文件系统搜索可能是可行的方法,而不是脆弱的单个文件夹检查。
像大多数事情一样,这完全取决于您想要实现的目标。
正如 EOL 在他的评论中指出的那样,Windows 比最初看起来要复杂一些。 他指向有关 Windows 的更完整文章的链接Desktop 文件夹包含有关桌面文件夹本地化的更多详细信息。对于国际应用程序的构建者来说,考虑这一点非常重要,要么使用内置于其工具集中的自动本地化,要么避免使用它的东西。
Underneath windows, the users home is
%HOMEPATH%
which is the equivalent of the linux and Mac~
. Underneath this, there is a folderDesktop
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.