谢谢你帮我
我正在尝试从python运行antiword以将.docx转换为.doc。我已将子流程用于该任务
import subprocess
test = subprocess.Popen(["antiword","/home/mypath/document.doc",">","/home/mypath/document.docx"], stdout=subprocess.PIPE)
output = test.communicate()[0]
但是它返回了错误
I can't open '>' for reading
I can't open '/home/mypath/document.docx' for reading
但同样的命令也适用于终端
antiword /home/mypath/document.doc > /home/mypath/document.docx
我做错了什么
shell将
>
字符解释为输出流重定向。但是,subprocess
不使用shell,因此没有任何东西可以将>
字符解释为重定向。因此>
字符将传递给命令。毕竟,它是一个完全合法的文件名:subprocess
怎么知道您实际上没有一个名为>
的文件不清楚为什么要尝试将
antiword
的输出重定向到文件,并读取变量output
中的输出。如果它被重定向到一个文件,在output
中将没有任何内容可读取如果要将
subprocess
调用的输出重定向到文件,请打开该文件以用Python编写,并将打开的文件传递给subprocess.Popen
:进程可能会写入其标准错误流,因此我在变量
error
中捕获了写入该错误流的任何内容相关问题 更多 >
编程相关推荐