Open explorer on a file
From Geoff Chappell's The Windows Explorer Command Line
import subprocesssubprocess.Popen(r'explorer /select,"C:\path\of\folder\file"')
A nicer and safer solution (only in Windows unfortunately) is os.startfile().
When it's given a folder instead of a file, it will open Explorer.
Im aware that i do not completely answer the question since its not selecting a file, but using subprocess
is always kind of a bad idea and this solution may help other people.
As explorer
could be overridden it would be a little safer to point to the executable directly. (just had to be schooled on this too)
And while you're at it: use Python 3s current subprocess API: run()
import osimport subprocessFILEBROWSER_PATH = os.path.join(os.getenv('WINDIR'), 'explorer.exe')def explore(path): # explorer would choke on forward slashes path = os.path.normpath(path) if os.path.isdir(path): subprocess.run([FILEBROWSER_PATH, path]) elif os.path.isfile(path): subprocess.run([FILEBROWSER_PATH, '/select,', path])