MichaelMao MichaelMao - 1 year ago 145
Python Question

Python call grep to fail

I want to execute this command in Python

grep keyMessage logFile.log > keyMessageFile.log

This is what I done now

from subprocess import call

keyMessage= 'keyMessage'
call(["grep", keyMessage, "logFile.log"])

but I don't know how to add the
> keyMessageFile.log

By the way, the reason why I use grep is because it's much faster than use read file then compare string then write file

There is the slower python code I write

keyMessage= 'keyMessage'

with open('logFile.log') as f:
for line in f:
with open(keyMessage+ '.txt', 'a') as newFile:
if(keyMessage not in line):

Answer Source

The simplest way to do this (reasonably safely too) is:

from subprocess import check_call
from shlex import quote
check_call('grep %s logFile.log > keyMessageFile.log' % quote(keyMessage), shell=True)

However unless you really need the regex matching capabilities of grep, and you end up reading keyMessageFile.log in your program anyway, I don't think the following would be unreasonably slow:

def read_matching_lines(filename, key):
    with open(filename) as fp:
      for line in fp:
        if key in line:
          yield line

for matching_line in read_matching_files('logFile.log', keyMessage):
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download