Python:执行 shell 命令

标签 python subprocess

我需要这样做:

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/

相关文章:

python - 无法使用 PyScript 访问文档

python - 在列表中查找第一个元素和索引匹配条件

python - Django REST - 使用序列化器创建带有外键的对象

python subprocess.Popen with shell = False,

python - 从 Python 运行 PowerShell 脚本

python - Rethinkdb 和 python 驱动程序-无法连接

python - 将注释掉的打印语句转换为记录调用 - Python

python - 从 Python 子进程执行 shell 脚本

python - 如何捕获子进程的 stdout 输出?

Python子进程通信,顶部显示CPU使用率低