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

时间:2022-08-06 00:12:51

How would one call a shell command from Python which contains a pipe and capture the output?

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

Suppose the command was something like:

假设命令是这样的:

cat file.log | tail -1

The Perl equivalent of what I am trying to do would be something like:

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

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

5 个解决方案

#1


40  

Use a subprocess.PIPE, as explained in the subprocess docs section "Replacing shell pipeline":

使用subprocess.PIPE,如子流程文档部分“替换shell管道”中所述:

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()

Or, using the sh module, piping becomes composition of functions:

或者,使用sh模块,管道成为功能组合:

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

#2


6  

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

Note that this does not capture stderr. And if you want to capture stderr as well, you'll need to use task.communicate(); calling task.stdout.read() and then task.stderr.read() can deadlock if the buffer for stderr fills. If you want them combined, you should be able to use 2>&1 as part of the shell command.

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

But given your exact case,

但鉴于你的具体情况,

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

avoids the need for the pipe at all.

根本不需要管道。

#3


1  

This:

import subprocess
p = subprocess.Popen("cat file.log | tail -1", shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
#Try shell=True if the above doesn't work with shell=False
p_stdout = p.stdout.read()
p_stderr = p.stderr.read()
print p_stdout

Or this should work:

或者这应该工作:

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

#4


1  

Another way similar to Popen would be:

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

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

#5


0  

This is a fork from @chown with some improvements:

这是@chown的一个分支,有一些改进:

  • an alias for import subprocess, makes easier when setting parameters
  • 导入子进程的别名,在设置参数时更容易

  • if you just want the output, you don't need to set stderr or stdin when calling Popen
  • 如果你只想要输出,那么在调用Popen时你不需要设置stderr或stdin

  • for better formatting, it's recommended to decode the output
  • 为了更好的格式化,建议解码输出

  • shell=True is necessary, in order to call an interpreter for the command line
  • 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]

#1


40  

Use a subprocess.PIPE, as explained in the subprocess docs section "Replacing shell pipeline":

使用subprocess.PIPE,如子流程文档部分“替换shell管道”中所述:

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()

Or, using the sh module, piping becomes composition of functions:

或者,使用sh模块,管道成为功能组合:

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

#2


6  

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

Note that this does not capture stderr. And if you want to capture stderr as well, you'll need to use task.communicate(); calling task.stdout.read() and then task.stderr.read() can deadlock if the buffer for stderr fills. If you want them combined, you should be able to use 2>&1 as part of the shell command.

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

But given your exact case,

但鉴于你的具体情况,

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

avoids the need for the pipe at all.

根本不需要管道。

#3


1  

This:

import subprocess
p = subprocess.Popen("cat file.log | tail -1", shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
#Try shell=True if the above doesn't work with shell=False
p_stdout = p.stdout.read()
p_stderr = p.stderr.read()
print p_stdout

Or this should work:

或者这应该工作:

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

#4


1  

Another way similar to Popen would be:

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

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

#5


0  

This is a fork from @chown with some improvements:

这是@chown的一个分支,有一些改进:

  • an alias for import subprocess, makes easier when setting parameters
  • 导入子进程的别名,在设置参数时更容易

  • if you just want the output, you don't need to set stderr or stdin when calling Popen
  • 如果你只想要输出,那么在调用Popen时你不需要设置stderr或stdin

  • for better formatting, it's recommended to decode the output
  • 为了更好的格式化,建议解码输出

  • shell=True is necessary, in order to call an interpreter for the command line
  • 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]