Python "show in finder"
How can I launch a new Finder window (or Explorer on Win) from python in a specific 开发者_StackOverflow社区folder. The behaviour I'm looking for is the equivalent of "Show in finder" link in a tracks context menu in iTunes or most other programs come to think of it.
What I have currently is a UI built with PyQt and I'd like to add a menu option like "show log folder" or something similar, which would pop up a new finder window.
UPDATE:
From katrielalex advice trying subprocess.Popen("/System/Library/CoreServices/Finder.app")
throws and OSError: Permission denied
. Trying to launch the Finder.app by double clicking it says that it is in use by OS X and cannot be opened.
Surely there must be a way to open a new Finder window.
For OS X, you can use the Finder's Apple Events (AppleScript) interface via py-appscript:
>>> from appscript import *
>>> file_to_show = "/Applications/iTunes.app"
>>> app("Finder").reveal(mactypes.Alias(file_to_show).alias)
app(u'/System/Library/CoreServices/Finder.app').startup_disk.folders[u'Applications'].application_files[u'iTunes.app']
>>> # Finder window of "Applications" folder appears with iTunes selected
EDIT:
An even simpler solution on OS X 10.6 is to use the new -R
(Reveal) option to the open
command (see man 1 open
):
>>> import subprocess
>>> file_to_show = "/Applications/iTunes.app"
>>> subprocess.call(["open", "-R", file_to_show])
from subprocess import call
targetDirectory = "~/Desktop"
call(["open", targetDirectory])
Windows:
>>> import subprocess
>>> subprocess.Popen( "explorer i:" )
<subprocess.Popen object at 0x00C46DB0>
For Finder the shell command
open ~/
would open a new window.
精彩评论