2016-11-28 48 views
-1

我有一个python程序,它会捕获一个文件,然后只有在输出中找到“oranges”这个词时才打印输出。我想知道如何反转这个,如果在输出中找到“oranges”,程序不输出任何内容?Python:使用子进程和Popen打印输出

#! /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 

回答

1

如何:

if not "oranges" in out: 
    print out