运行包含管道的命令行并将结果显示到STDOUT

时间:2011-09-08 18:54:28

标签: python command-line pipe

如何从Python调用包含管道的shell命令并捕获输出?

假设命令类似于:

cat file.log | tail -1

Perl相当于我想要做的事情就像:

my $string = `cat file.log | tail -1`;

6 个答案:

答案 0 :(得分:46)

使用subprocess.PIPE,如子流程文档部分"Replacing shell pipeline"中所述:

import subprocess
p1 = subprocess.Popen(["cat", "file.log"], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["tail", "-1"], stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
output,err = p2.communicate()

或者,使用sh module,管道变为composition of functions

import sh
output = sh.tail(sh.cat('file.log'), '-1')

答案 1 :(得分:7)

import subprocess
task = subprocess.Popen("cat file.log | tail -1", shell=True, stdout=subprocess.PIPE)
data = task.stdout.read()
assert task.wait() == 0

请注意,这不会捕获stderr。如果你想捕获stderr,你需要使用task.communicate();如果stderr的缓冲区填满,则调用task.stdout.read()然后task.stderr.read()会死锁。如果你想要它们的组合,你应该能够使用2>&1作为shell命令的一部分。

但鉴于你的具体情况,

task = subprocess.Popen(['tail', '-1', 'file.log'], stdout=subprocess.PIPE)
data = task.stdout.read()
assert task.wait() == 0

根本不需要管道。

答案 2 :(得分:2)

此:

import subprocess
p = subprocess.Popen("cat file.log | tail -1", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
#for shell=False use absolute paths
p_stdout = p.stdout.read()
p_stderr = p.stderr.read()
print p_stdout

或者这应该有效:

import os
result = os.system("cat file.log | tail -1")

答案 3 :(得分:1)

与Popen类似的另一种方式是:

command=r"""cat file.log | tail -1 """
output=subprocess.check_output(command, shell=True)

答案 4 :(得分:0)

这是来自 @chown 的分支,但有一些改进:

  • import subprocess的别名,在设置参数时更容易
  • 如果您只想要输出,则在调用stderr
  • 时不需要设置stdinPopen
  • 为了更好的格式化,建议解码输出
  • shell=True是必要的,以便为命令行调用解释器
#!/usr/bin/python3

import subprocess as sp

p = sp.Popen("cat app.log | grep guido", shell=True, stdout=sp.PIPE)

output = p.stdout.read()
print(output.decode('utf-8'))
$ cat app.log 
2017-10-14 22:34:12, User Removed [albert.wesker]
2017-10-26 18:14:02, User Removed [alexei.ivanovich] 
2017-10-28 12:14:56, User Created [ivan.leon]
2017-11-14 09:22:07, User Created [guido.rossum]

$ python3 subproc.py 
2017-11-14 09:22:07, User Created [guido.rossum]

答案 5 :(得分:0)

具有许多管道的运行shell命令的简单功能

使用

res, err = eval_shell_cmd('pacman -Qii | grep MODIFIED | grep -v UN | cut -f 2')

功能

import subprocess


def eval_shell_cmd(command, debug=False):
    """
    Eval shell command with pipes and return result
    :param command: Shell command
    :param debug: Debug flag
    :return: Result string
    """
    processes = command.split(' | ')

    if debug:
        print('Processes:', processes)

    for index, value in enumerate(processes):
        args = value.split(' ')

        if debug:
            print(index, args)

        if index == 0:
            p = subprocess.Popen(args, stdout=subprocess.PIPE)
        else:
            p = subprocess.Popen(args, stdin=p.stdout, stdout=subprocess.PIPE)

        if index == len(processes) - 1:
            result, error = p.communicate()
            return result.decode('utf-8'), error