“错误”;sh:1:改为:非法选项-d“;在系统命令中使用here document时,请从Python运行
我在Python中运行Bash系统命令,在使用“错误”;sh:1:改为:非法选项-d“;在系统命令中使用here document时,请从Python运行,python,bash,heredoc,Python,Bash,Heredoc,我在Python中运行Bash系统命令,在使用read在命令中定义here文档时遇到了这个问题: import os text = "~|||-:this is text:-|||~" fileName = "sound.wav" command =\ "IFS= read -d \'\' text <<EOF\n" +\ text + "\n" +\ "EOF\n" +\ "echo \"${text}\" | text2wave -sca
read
在命令中定义here文档时遇到了这个问题:
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)
导入操作系统
text=“~| | |-:这是text:-| | | ~”
fileName=“sound.wav”
命令=\
“IFS=read-d\'\'text一种更具python风格的方法是使用,通过指定stdin
to或使用
另外,-d
在您的read
版本中可能不是有效选项。帮助[r]ead
告诉您什么?没有理由在shell中执行所有这些操作。Python可以直接写入将运行text2wave
的进程的标准输入。下面是一个使用子进程模块的示例
p = subprocess.Popen(["text2wave", "-scale", "1", "-o", filename], stdin=subprocess.PIPE)
p.stdin.write(text + "\n")
p.wait()
谢谢你的回答。我想我的问题在于调用shell时使用读取的方式,而不是我没有使用子进程。我会使用子进程,但在其中实现管道需要很多代码,我将使用多个管道。将管道与一起使用要简洁得多>操作系统
方法。
p = subprocess.Popen(["text2wave", "-scale", "1", "-o", filename], stdin=subprocess.PIPE)
p.stdin.write(text + "\n")
p.wait()