2016-11-28 5 views
-1

파일에 cat을 사용하고 출력에 "oranges"라는 단어가있는 경우에만 출력을 인쇄하는 python 프로그램이 있습니다. 나는 이걸 뒤집을 수 있을지 궁금해서 "오렌지"가 출력에서 ​​발견되면 프로그램에 아무 것도 출력하지 않겠습니까?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