我需要这样做:
paste file1 file2 file3 > result
我的 python 脚本中有以下内容:
from subprocess import call
// other code here.
// Here is how I call the shell command
call ["paste", "file1", "file2", "file3", ">", "result"])
不幸的是我得到了这个错误:
粘贴:>:没有那个文件或目录。
这方面的任何帮助都会很棒!
最佳答案
如果您明智地决定不使用 shell,则需要自己实现重定向。
文档位于 https://docs.python.org/2/library/subprocess.html警告您不要使用管道——但是,您不需要:
import subprocess
with open('result', 'w') as out:
subprocess.call(["paste", "file1", "file2", "file3"], stdout=out)
应该没问题。
关于Python:执行 shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28468807/