简体   繁体   English

Python有没有跨平台打开文件浏览器的方法?

[英]Is there a cross-platform way to open a file browser in Python?

I'm thinking something along the lines of the webbrowser module, but for file browsers.我正在考虑与webbrowser模块类似的东西,但适用于文件浏览器。 In Windows I'd like to open explorer, in GNOME on Linux I want to open nautilus, Konqueror on KDE, etc. I'd prefer not to kludge it up if I can avoid it.在 Windows 中,我想打开资源管理器,在 Linux 中的 GNOME 中,我想在 KDE 上打开 nautilus、Konqueror 等。如果可以避免的话,我宁愿不把它弄乱。 ;-) ;-)

I'd prefer not to kludge it up if I can avoid it. 如果我可以避免它,我宁愿不把它搞砸。

Weeell I think you are going to need a little bit of platform-sniffing kludge, but hopefully not as much as the ghastly command-sniffing webbrowser module. Weeell我认为你需要一点平台嗅探kludge,但希望不如可怕的命令嗅探webbrowser模块。 Here's a first stab at it: 这是对它的第一次尝试:

if sys.platform=='win32':
    subprocess.Popen(['start', d], shell= True)

elif sys.platform=='darwin':
    subprocess.Popen(['open', d])

else:
    try:
        subprocess.Popen(['xdg-open', d])
    except OSError:
        # er, think of something else to try
        # xdg-open *should* be supported by recent Gnome, KDE, Xfce

Note the win32 version will currently fail for spaces in filenames. 请注意,对于文件名中的空格,win32版本当前将失败。 Bug 2304 might be something to do with that, but there does seem to be a basic problem with parameter escaping and the Windows shell ( cmd /c ... ), in that you can't nest double-quotes and you can't ^-escape quotes or spaces. 错误2304可能与此有关,但似乎存在参数转义和Windows shell( cmd /c ... )的基本问题,因为你不能嵌套双引号而你不能^ -escape引号或空格。 I haven't managed to find any way to quote and run cmd /c start C:\\Documents and Settings from the command line at all. 我还没有找到任何方法来引用和运行cmd /c start C:\\Documents and Settings从命令行运行cmd /c start C:\\Documents and Settings

ETA re nosklo's comment: on Windows only, there is a built-in way to do it: ETA re nosklo的评论:仅在Windows上,有一种内置方式可以做到:

if sys.platform=='win32':
    os.startfile(d)

Here's the not-very-nice alternative solution to find the shell and open a folder with it, which you shouldn't now need, but I'll leave in. (Partly because it might be of use for something else, but mostly because I spent the time to type the damned thing!) 这是找到shell并用它打开一个文件夹的不太好的替代解决方案,你现在不需要它,但我会留下来。(部分原因是因为它可能用于别的东西,但主要是因为我花时间打死该死的东西!)

if sys.platform=='win32':
    import _winreg
    path= r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon')
    for root in (_winreg.HKEY_CURRENT_USER, _winreg.HKEY_LOCAL_MACHINE):
        try:
            with _winreg.OpenKey(root, path) as k:
                value, regtype= _winreg.QueryValueEx(k, 'Shell')
        except WindowsError:
            pass
        else:
            if regtype in (_winreg.REG_SZ, _winreg.REG_EXPAND_SZ):
                shell= value
            break
    else:
        shell= 'Explorer.exe'
    subprocess.Popen([shell, d])

Just came accross this post with a similar problem Choosing a file in Python with simple Dialog .刚遇到这篇文章时遇到了类似的问题Choosing a file in Python with simple Dialog I recommend going there for examples and code snippets.我建议去那里查看示例和代码片段。 Basically 2 suggestions were offered:基本上提供了2个建议:

  1. Using tkinter : that appears to compatible with macOS and Windows, also is very mainstream so there is a lot written about it.使用tkinter :似乎与 macOS 和 Windows 兼容,也是非常主流的,所以有很多关于它的文章。
  2. Using plyer : Seems like a newer library and the degree of supports in different OS varies significantly.使用plyer :似乎是一个较新的库,不同操作系统的支持程度差异很大。

I would try tkinter first and then if that fails try one of the alternatives.我会先尝试 tkinter,如果失败,请尝试其中一种选择。

Since this thread is very old, is probably worth sharing newer alternatives that may have came up more recently for other people looking for answers to the same question.由于这个线程很旧,可能值得分享更新的替代方案,这些替代方案可能是最近出现的,供其他人寻找同一问题的答案。

This is a complete stab in the dark, but take a look at wxPython which provides Python bindings to the underlying wxWidgets library. 这是一个彻头彻尾的黑暗,但请看一下wxPython ,它提供了与底层wxWidgets库的Python绑定。 It has been a long time since I last looked at it, but there might be something there that you can use. 自从我上次查看它以来已经很长时间了,但可能会有一些你可以使用的东西。 Otherwise, it should be relatively easy to make your own file browser that will use the native "widgets" for each OS. 否则,制作自己的文件浏览器应该相对容易,该浏览器将为每个操作系统使用本机“小部件”。

Mind you, wxPython is not light weight, it will really bulk up your application and increase your dependencies. 请注意,wxPython不是轻量级的,它会真正增加您的应用程序并增加您的依赖关系。

I don't know if a ready-to-use module exists, but if there is, it should be on the Activestate's python cookbok ( http://code.activestate.com/recipes/langs/python/ ) 我不知道是否存在一个现成的模块,但如果有,它应该在Activestate的python cookbok上( http://code.activestate.com/recipes/langs/python/

Also, at least in gnome and on mac os, you can execute "gnome-open http://blah " and "open http://blah " (on mac); 另外,至少在gnome和mac os上,你可以执行“gnome-open http:// blah ”和“open http:// blah ”(在mac上); both will open the url in user's preferred browser. 两者都将打开用户首选浏览器中的URL。

For linux also check freedesktop.org -- a common set of tools covering both Gnome and KDE, that should include something similar. 对于linux,还要检查freedesktop.org--一个涵盖Gnome和KDE的常用工具集,应该包含类似的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 轻量级跨平台方式提示文件 - Lightweight cross-platform way to prompt for a file python的pickle文件是跨平台的吗? - Is pickle file of python cross-platform? 如何以跨平台方式在Python中打开带有主题的默认邮件程序? - How do I open the default mail program with a subject in Python in a cross-platform way? 如何在Python中使用装饰器跨平台/浏览器运行测试? - How to run tests cross-platform/browser using decorator in Python? Python中如何判断一个不存在的路径是否以跨平台的方式通向一个目录或一个文件? - How to tell if a non-existent path leads to a directory or a file in a cross-platform way in Python? 跨平台Python可执行文件 - Cross-platform Python Executables 使用 execv 运行时指定 Python 解释器的跨平台方式 - Cross-platform way to specify Python interpreter when running with execv Python:以跨平台方式获取AppData文件夹 - Python: Getting AppData folder in a cross-platform way 是否存在从Python的OSError获取信息的跨平台方式? - Is there a cross-platform way of getting information from Python's OSError? 使用Python插件可扩展性编写跨平台应用程序的最简单方法? - Simplest way to write cross-platform application with Python plugin extensibility?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM