running a command line containing Pipes and displaying result to STDOUT
Use a subprocess.PIPE, as explained in the subprocess docs section "Replacing shell pipeline":
import subprocessp1 = 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:
import shoutput = sh.tail(sh.cat('file.log'), '-1')
import subprocesstask = 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.
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.
This:
import subprocessp = subprocess.Popen("cat file.log | tail -1", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)#for shell=False use absolute pathsp_stdout = p.stdout.read()p_stderr = p.stderr.read()print p_stdout
Or this should work:
import osresult = os.system("cat file.log | tail -1")