user5578188 user5578188 - 2 years ago 109
Python Question

Python: Using subprocess and Popen to Print output

I have a python program that will cat a file and then print out the output only if the word "oranges" is found in the output. I was wondering how I could reverse this and have the program NOT output anything if "oranges" is found in the output?

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

Answer Source

How about:

if not "oranges" in out:
    print out
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download