python - babel python check_output错误非零退出状态2

标签 python zsh babeljs

在 python 中,我有以下行:

check_output(['babel', 'www/scripts6/lib/data.js', '>', 'www/scripts/lib/data.js'])

它给了我错误:

subprocess.CalledProcessError: Command '['babel', 'www/scripts6/lib/data.js', '>', 'www/scripts/lib/data.js']' returned non-zero exit status 2

我被难住了。该命令在 shell 中运行良好。为什么 python 会提示?

编辑:使用

print(check_output(['babel', './www/scripts6/lib/data.js', '>', './www/scripts/lib/data.js'], shell=True))

而是产生输出

b'"use strict";\n\n'

在终端中。这表明 babel 现在正在工作,但 babel 不接受任何内容作为其输入并输出到终端。

最佳答案

您应该将 shell 命令作为单个字符串传递给 check_outputdocumentation关于这个函数实际上给出了一个例子来做到这一点。在你的情况下,它看起来像这样:

check_output('path/to/babel ./www/scripts6/lib/data.js > '
             './www/scripts/lib/data.js', shell=True)

documentation关于 Popen 状态:

On POSIX with shell=True, the shell defaults to /bin/sh. If args is a string, the string specifies the command to execute through the shell. This means that the string must be formatted exactly as it would be when typed at the shell prompt. This includes, for example, quoting or backslash escaping filenames with spaces in them. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself. That is to say, Popen does the equivalent of:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

check_outputshell=True 以与上述相同的方式解释其参数。

如果您仅使用 shell 进行重定向,我建议您这样做:

check_output(['path/to/babel', './www/scripts6/lib/data.js', 
              '--out-file', './www/scripts/lib/data.js'])

这会将输出保存在您想要的文件中,而无需生成 shell。

关于python - babel python check_output错误非零退出状态2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32491194/

相关文章:

python - ld 找不到 .so 库

Python代码根据售出的书籍数量查找所有人

python - Pandas Multiindex - 对组执行操作

javascript - react native 启动意外 token

python - 如何在python中设置多个循环 'for'作为函数的参数来找到将一个句点切成n份的所有可能性?

zsh - 无法更改用于 zsh 提示的函数中的全局变量

shell - 从终端在 vim 中即时编辑 shell 命令

linux - zsh compinit : insecure directories. Compaudit 显示/tmp 目录

javascript - Babel 代码抛出超出最大调用堆栈大小

reactjs - 带有React和Babel语法错误的Brunch.io