gpt4 book ai didi

python -/bin/sh : line 62: to: command not found

转载 作者:可可西里 更新时间:2023-11-01 16:42:22 24 4
gpt4 key购买 nike

我有一个 python 代码,我在其中调用了一个 shell 命令。我执行 shell 命令的代码部分是:

try:
def parse(text_list):
text = '\n'.join(text_list)
cwd = os.getcwd()
os.chdir("/var/www/html/alenza/hdfs/user/alenza/sree_account/sree_project/src/core/data_analysis/syntaxnet/models/syntaxnet")
synnet_output = subprocess.check_output(["echo '%s' | syntaxnet/demo.sh 2>/dev/null"%text], shell = True)
os.chdir(cwd)
return synnet_output
except Exception as e:
sys.stdout.write(str(e))

现在,当我使用一些示例输入在本地文件上运行此代码时(我执行了 cat/home/sree/example.json | python parse.py)它工作正常并且我得到了所需的输出。但我正在尝试使用我的 HDFS 上的输入运行代码(相同的 cat 命令,但输入文件路径来自 HDFS),它包含完全相同类型的 json 条目,但失败并出现错误:

/bin/sh: line 62: to: command not found
list index out of range

我在 Stack Overflow 上阅读了类似的问题,解决方案是为正在调用的 shell 脚本包含一个 Shebang 行。我在 demo.sh 脚本中有 shebang 行 #!/usr/bin/bash

此外,which bash 给出了 /usr/bin/bash

有人请详细说明。

最佳答案

您很少(如果有的话)想要将传递列表参数与 shell=True 结合起来。只需传递字符串:

synnet_output = subprocess.check_output("echo '%s' | syntaxnet/demo.sh 2>/dev/null"%(text,), shell=True)

但是,您实际上并不需要此处的 shell 管道。

from subprocess import check_output
from StringIO import StringIO # from io import StringIO in Python 3
synnet_output = check_output(["syntaxnet/demo.sh"],
stdin=StringIO(text),
stderr=os.devnull)

关于python -/bin/sh : line 62: to: command not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39619221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com