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



Solution 1:[1]

Just came accross this post with a similar problem Choosing a file in Python with simple Dialog. I recommend going there for examples and code snippets. Basically 2 suggestions were offered:

  1. Using tkinter: that appears to compatible with macOS and Windows, also is very mainstream so there is a lot written about it.
  2. Using plyer: Seems like a newer library and the degree of supports in different OS varies significantly.

I would try tkinter first and then if that fails try one of the alternatives.

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.

Solution 2:[2]

This is a complete stab in the dark, but take a look at wxPython which provides Python bindings to the underlying wxWidgets library. 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.

Solution 3:[3]

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

Also, at least in gnome and on mac os, you can execute "gnome-open http://blah" and "open http://blah" (on mac); both will open the url in user's preferred browser.

For linux also check freedesktop.org -- a common set of tools covering both Gnome and KDE, that should include something similar.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bruno Castro
Solution 2 mhawke
Solution 3 anonymous