Skip to main content
 首页 » 编程设计

Python:执行 shell 命令

2025年05月04日12emanlee

我需要这样做:

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) 

应该没问题。