user5578188 user5578188 - 6 days ago 5
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

How about:

if not "oranges" in out:
    print out
Comments