user5578188 user5578188 - 1 month ago 11
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


How about:

if not "oranges" in out:
    print out