running a command line containing Pipes and displaying result to STDOUT

前端 未结 6 970
不思量自难忘°
不思量自难忘° 2020-12-01 06:20

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

Suppose the command was something like:

cat file.log |          


        
6条回答
  •  Happy的楠姐
    2020-12-01 06:31

    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.

    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.

提交回复
热议问题