Python:使用子进程和Popen打印输出

Python:使用子进程和Popen打印输出,python,linux,subprocess,popen,shlex,Python,Linux,Subprocess,Popen,Shlex,我有一个python程序,它将对一个文件进行cat,然后仅当在输出中找到单词“oranges”时才打印输出。我想知道,如果在输出中发现“橙子”,我怎么能扭转这种情况,让程序不输出任何东西 #! /usr/bin/python import commands, os, string import sys import fileinput import subprocess from subprocess import Popen, PIPE import shlex cmd =

我有一个python程序,它将对一个文件进行cat,然后仅当在输出中找到单词“oranges”时才打印输出。我想知道,如果在输出中发现“橙子”,我怎么能扭转这种情况,让程序不输出任何东西

#! /usr/bin/python

import commands, os, string
import sys
import fileinput
import subprocess
from subprocess import Popen, PIPE
import shlex

        cmd = "cat /root/newfile.txt"

        args = shlex.split(cmd)

        p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = p.communicate()
        if out.find("oranges") > -1:
                print out
那么:

if not "oranges" in out:
    print out