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

Python subprocess call throws error when writing file

我想使用 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."'])