Python 子进程调用在写入文件时抛出错误

标签 python linux python-2.7 ubuntu text-to-speech

我想使用 SVOX/pico2wave 从 Python 代码编写一个 wav 文件。当我从终端执行这一行时,文件写得很好:

/usr/bin/pico2wave -w=/tmp/tmp_say.wav "Hello world."

我已验证 pico2wave 位于 /usr/bin

这是我的 Python 代码:

from subprocess import call

call('/usr/bin/pico2wave -w=/tmp/tmp_say.wav "Hello world."')

... 抛出此错误:

Traceback (most recent call last):
  File "app/app.py", line 63, in <module>
    call('/usr/bin/pico2wave -w=/tmp/tmp_say.wav "Hello world."')
  File "/usr/lib/python2.7/subprocess.py", line 168, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 390, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1024, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

最佳答案

来自documentation

Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

所以你可以试试

call(['/usr/bin/pico2wave', '-w=/tmp/tmp_say.wav', '"Hello world."'])

关于Python 子进程调用在写入文件时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43527859/

相关文章:

python - Python 中是否可以换行注释?

python - Matplotlib imshow 缩放功能?

更改目标ip

python-2.7 - 为 ssl 包装的套接字调用连接方法时出现一些错误

python - Argparse 与函数的交互

python - 在python中使用下划线作为循环变量是否可以接受?

linux - 如何使用 sed 删除双换行符?

c - 如果输出到终端,则在 C 中检测

python - 将日期范围与 Python 日期列表中的索引进行匹配的快速方法

python - 当程序终止或 pc 关闭时调用函数