2016-11-28 47 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