使用python中的ruby gem/command

2024-06-07 12:20:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在我的mac电脑上安装了Ruby gem'haml',我可以使用它在终端使用以下命令将haml文件编译成html文件:

haml 'path/to/haml/file.haml' 'desired/html/path/file.html'

这个命令只是在第二个路径上创建一个html文件,并且在终端中不给出任何输出。例如,这个命令:

^{pr2}$

创建一个糖.html文件位于给定路径。现在我尝试从python脚本中使用这个功能。当我在IDLE的交互式python shell中输入以下内容时:

>>>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终端使用的,所以我把它也放进去了。在

听了伊克图菲的建议,我打了个查查电话。这是我收到的回溯:

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.

不过,我发现shell在查找之前并不是一个可执行的命令,而是因为它在查找之前并不是一个可执行的命令,而是因为它确实是一个可执行的命令。在

现在如何让python找到这个haml命令?或者我必须使用一些难看的解决方法,比如applescript,然后调用haml命令。在


Tags: 文件命令终端htmlshellsugaruserssites
2条回答

我看到您正在使用,shell=True,所以我本以为一切都能正常工作。在这里用python2.7.1和haml3.1.1在本地检查了它,我在执行它时没有问题。还有一些python实现您可能感兴趣,PyHAMLHamlPydjaml或{a4}。在

import subprocess
subprocess.Popen(['haml', 'hello.haml', 'hello.html'], shell=True)

% cat hello.html
<strong class='code' id='message'>Hello, World!</strong>

当你想建立适合Popen的列表时,shlex.split()是你的朋友。在

>>> import subprocess
>>> import shlex
>>> p = subprocess.Popen(shlex.split('haml "/Volumes/Macintosh HD/Users/me/Sites/ICSP/sugar.haml" "/Volumes/Macintosh HD/Users/me/Sites/ICSP/sugar.html"'))
>>> p.wait()
0

相关问题 更多 >

    热门问题