[英]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个建议:
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.