Skip to content
Advertisement

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

Advertisement

Answer

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. Here’s a first stab at it:

JavaScript

Note the win32 version will currently fail for spaces in filenames. 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. 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.

ETA re nosklo’s comment: on Windows only, there is a built-in way to do it:

JavaScript

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

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement