我已经安装在我的MAC,我可以用在终端使用下面的命令来编译HAML文件转换为HTML文件的红宝石宝石“HAML”使用Ruby宝石/命令该命令仅在第二个路径创建一个html文件,并且在终端中不提供输出。因此,例如,此命令:蟒蛇
haml "/Volumes/Macintosh HD/Users/me/Sites/ICSP/sugar.haml" "/Volumes/Macintosh HD/Users/me/Sites/ICSP/sugar.html"
在给定路径处创建一个sugar.html文件。现在我试图从python脚本中使用这个功能。当我键入此为IDLE的交互式Python外壳:
>>>import subprocess
>>>subprocess.Popen('haml "/Volumes/Macintosh HD/Users/me/Sites/ICSP/sugar.haml" "/Volumes/Macintosh HD/Users/me/Sites/ICSP/sugar.html"', shell=True, executable='/bin/bash')
<subprocess.Popen object at 0x159d6f0>
我得到的输出表明进程已经运行,但没有输出文件。这是为什么发生?我甚至放入了Shell参数,但没有出现交互式shell。另外,我在某处读到,使用的默认shell不是bash,这是Mac终端使用的,所以我把它放在了太好的位置。
按照icktoofay的建议,我跑了check_call。这是我收到的回溯:
Traceback (most recent call last):
File "/Users/neil/Desktop/subprocesstest.py", line 7, in p = subprocess.check_call(x, shell=True) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 504, in check_call raise CalledProcessError(retcode, cmd) CalledProcessError: Command 'haml "/Volumes/Macintosh HD/Users/neil/Sites/ICSP/sugar.haml" "/Volumes/Macintosh HD/Users/neil/Sites/ICSP/sugar.html"' returned non-zero exit status 127
按照bash reference manual,而寻找一个要执行的命令,
If the name is neither a shell function nor a builtin, and contains no slashes, Bash searches each element of $PATH for a directory containing an executable file by that name. ... If that function is not defined, the shell prints an error message and returns an exit status of 127.
但是,我认为这将后壳确实找到HAML命令和可执行参数,因为在此之前,它给出'文件或目录未找到错误',这表明该函数不是直接可执行的,而是仅在一个shell中可执行。
现在我该如何让python找到这个haml命令?或者,我会不得不使用一些丑陋的解决方法,像一个然后调用haml命令的applescript。
您是否尝试过使用['subprocess.check_call'(http://docs.python.org/library/subprocess.html#subprocess.check_call),而不是直接使用['subprocess.Popen']( http://docs.python.org/library/subprocess.html#subprocess.Popen)? – icktoofay 2011-06-04 20:37:55
对不起,现在这个问题似乎很长时间了,但我试图提供尽可能多的信息来使调试更容易。 – Neil 2011-06-04 20:59:10
你可能已经尝试过这样的事情,但是这个工作(路径明显改变了)? 'subprocess.check_call(['/ path/to/haml','/your/file.haml','/your/file.html'])' – icktoofay 2011-06-04 21:03:38