Python “show in finder”

前端 未结 4 986
梦如初夏
梦如初夏 2020-12-31 10:51

How can I launch a new Finder window (or Explorer on Win) from python in a specific folder. The behaviour I\'m looking for is the equivalent of \"Show in finder\" link in a

相关标签:
4条回答
  • 2020-12-31 11:30

    Windows:

    >>> import subprocess
    >>> subprocess.Popen( "explorer i:" )
    <subprocess.Popen object at 0x00C46DB0>
    
    0 讨论(0)
  • 2020-12-31 11:43
    from subprocess import call
    targetDirectory = "~/Desktop"
    call(["open", targetDirectory])
    
    0 讨论(0)
  • 2020-12-31 11:43

    For Finder the shell command

    open ~/
    

    would open a new window.

    0 讨论(0)
  • 2020-12-31 11:47

    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])
    
    0 讨论(0)
提交回复
热议问题