17

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

2
  • Since "file browser" is not a cross-platform feature, what -- specifically -- are you talking about? Are you talking about some kind of GUI window that is "cross-platform"? What GUI toolkit's have you looked at that meet your definition of "cross-platform"? Commented Nov 25, 2009 at 11:46
  • 1
    @S.Lott: Not a Python-process owned GUI window -- shelling out to a native subprocess in the same sense that webbrowser does, appropriate to the user's operating environment. Commented Nov 25, 2009 at 21:27

4 Answers 4

24

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:

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. 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:

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

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])
Sign up to request clarification or add additional context in comments.

2 Comments

It would be nice to have this globally supported, as per bugs.python.org/issue3177
Both os.startfile and the long registry/subprocess method still work for me in Windows 7.
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.

Comments

-1

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.

Comments

-2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.