gpt4 book ai didi

python - 在从 Python 运行的系统命令中使用此处文档时出现错误 "sh: 1: read: Illegal option -d"

转载 作者:太空宇宙 更新时间:2023-11-03 17:59:54 25 4
gpt4 key购买 nike

我正在 Python 中运行 Bash 系统命令,并且在使用 read 在命令中定义此处文档时遇到了此问题:

import os
text = "~|||-:this is text:-|||~"
fileName = "sound.wav"
command =\
"IFS= read -d \'\' text <<EOF\n" +\
text + "\n" +\
"EOF\n" +\
"echo \"${text}\" | text2wave -scale 1 -o " + fileName
os.system(command)

你能帮我解决这个问题吗?

<小时/>

这是一个稍微简化的版本:

import os
text = "~|||-:this is text:-|||~"
command =\
"IFS= read -d \'\' text <<EOF\n" +\
text + "\n" +\
"EOF\n" +\
"echo \"${text}\""
os.system(command)

我想在上面的内容中明确表示我将使用管道。当我运行此程序时,出现以下错误:

sh: 1: 读取:非法选项 -d

最佳答案

没有理由在 shell 中执行所有这些操作。 Python 可以直接写入将运行 text2wave 的进程的标准输入。以下是使用 subprocess 模块的示例。

p = subprocess.Popen(["text2wave", "-scale", "1", "-o", filename], stdin=subprocess.PIPE)
p.stdin.write(text + "\n")
p.wait()

关于python - 在从 Python 运行的系统命令中使用此处文档时出现错误 "sh: 1: read: Illegal option -d",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27874681/

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