使用 python 中的 ruby gem/命令
我已经在我的 mac 上安装了 Ruby gem 'haml',我可以使用它在终端使用以下命令将 haml 文件编译为 html 文件:
haml 'path/to/haml/file.haml' 'desired/html/path/file.html'
此命令只是在第二个路径中创建一个 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 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 终端使用的是 bash,因此我也将其放入以进行良好的测量。
按照 icktoofay 的建议,我运行了 check_call。这是我收到的回溯:
回溯(最近一次调用最后一次):
文件 “/Users/neil/Desktop/subprocesstest.py”, 7号线,在 p = subprocess.check_call(x, shell=True) 文件 “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py”, check_call 中的第 504 行 引发 CalledProcessError(retcode, cmd) CalledProcessError: 命令 'haml “/卷/Macintosh HD/Users/neil/Sites/ICSP/sugar.haml" “/卷/Macintosh HD/Users/neil/Sites/ICSP/sugar.html"' 返回非零退出状态 127
根据 bash 参考手册,搜索要执行的命令,
如果名称既不是 shell 函数也不是内置函数,并且包含 没有斜杠,Bash 搜索每个元素 $PATH 的目录包含 具有该名称的可执行文件。 ... 如果 该函数未定义, shell 打印一条错误消息并 返回退出状态 127。
但是,我认为在添加 shell 和可执行参数后确实找到了 haml 命令,因为在此之前,它给出了“文件或目录未找到错误”,这表明该函数不是可以直接执行,但只能在 shell 中执行。
现在我如何让 python 找到这个 haml 命令?或者我是否必须使用一些丑陋的解决方法,例如 applescript,然后调用 haml 命令。
I've installed the Ruby gem 'haml' on my mac, which I can use to compile haml files into html files using the following command at the terminal:
haml 'path/to/haml/file.haml' 'desired/html/path/file.html'
This command simply creates an html file at the second path, and gives no output in the terminal. So for example, this command:
haml "/Volumes/Macintosh HD/Users/me/Sites/ICSP/sugar.haml" "/Volumes/Macintosh HD/Users/me/Sites/ICSP/sugar.html"
Creates a sugar.html file at the given path. Now I'm trying to use this functionality from a python script. When I type this into IDLE's interactive 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>
I get output suggesting that the process has been run, however, there is no file outputted. Why is this happening? I even put in the Shell argument, but no interactive shell shows up. Also, I read somewhere that the default shell used is not bash, which is what the Mac terminal uses, so I put that in too for good measure.
Following icktoofay's advice, I ran check_call. Here is the traceback I received:
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
According to the bash reference manual, while searching for a command to be executed,
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.
However, I thought it was indeed finding the haml command after adding the shell and executable arguments, because before that, it was giving a 'file or directory not found error', which indicates that the function is not executable directly but rather in a shell only.
Now how do I make python find this haml command? Or would I have to use some ugly workaround like an applescript which then invokes the haml command.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到您正在使用
shell=True
,所以我希望一切都能正常工作。使用 Python 2.7.1 和 haml 3.1.1 在本地检查它,执行它没有任何问题。还有一些您可能感兴趣的 Python 实现,PyHAML、HamlPy、djaml 或 < a href="https://github.com/fitoria/django-haml" rel="nofollow">django-haml。I see that you are using,
shell=True
, so I would have expected things to just work. Checked it locally here with Python 2.7.1 and haml 3.1.1 and I had no problems executing it. There are also some python implementations you might be interested in, PyHAML, HamlPy, djaml or django-haml.shlex.split() 是你的朋友,当你想要构建 < code>args 列表适用于
Popen
及其同类。shlex.split() is your friend, when you want to build
args
list suitable forPopen
and its ilk.