如何在Python 2.7(OS X)中获取和使用STDOUT中的路径



这个问题与使用OS X的人有关,因为我使用AppleScript从最重要的Finder窗口返回路径。我想获取此路径并列出目录内容。最终,我将使用输出来重命名文件。

出于某种原因,下面的代码产生了OSError: 2, 'No such file or directory'

这是代码:

import os
from subprocess import Popen,PIPE,STDOUT,call 
def ascript():
    cmd = """osascript -e 'tell app "Finder" to get the quoted form of the POSIX path of (target of window 1 as alias) '"""
    proc=Popen(cmd, shell=True, stdout=PIPE, ) 
    output,err=proc.communicate()
    return output.rstrip()
stuff = os.listdir(ascript())

我也尝试过:

path = ascript()
stuff = os.listdir(path)

您不需要返回 POSIX 路径的引用形式。当你这样做时,python实际上是在阅读:

'/path/to/window'

删除quoted form时,它将返回正确的路径格式:

/path/to/window

最新更新