Re: [Tutor] Grepping a file for words in a list

2006-02-26 Thread Alan Gauld

"John Purser" <[EMAIL PROTECTED]> wrote in message
> I'm writing a system admin script in python that checks recently
> accessed files for keywords like "failed, denied, error,..." etc.  I'm
> using popen to call grep -F   but it's VERY slow.  Can
> anyone suggest a faster method to do this?

are you trying to locate the actual strings in the files or only testing to
see if the strings occur? If the latter then you could use something like

files = [f for f in glob(pattern) if mystring in open(f).read()]

or for multiple patterns construct a single regex:

files = [f for f in glob(pattern) if regex.search(open(f).read())]

That might be slightly faster than spawning sub processes with popen.
But it won't be a radical difference and if you actually want the line
numbers of the patterns I suspect grep will be at least as fast as
using python.

HTH,

-- 
Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Grepping a file for words in a list

2006-02-25 Thread Alan Gauld

"John Purser" <[EMAIL PROTECTED]> wrote in message
> I'm writing a system admin script in python that checks recently
> accessed files for keywords like "failed, denied, error,..." etc.  I'm
> using popen to call grep -F   but it's VERY slow.  Can
> anyone suggest a faster method to do this?

are you trying to locate the actual strings in the files or only testing to
see if the strings occur? If the latter then you could use something like

files = [f for f in glob(pattern) if mystring in open(f).read()]

or for multiple patterns construct a single regex:

files = [f for f in glob(pattern) if regex.search(open(f).read())]

That might be slightly faster than spawning sub processes with popen.
But it won't be a radical difference and if you actually want the line
numbers of the patterns I suspect grep will be at least as fast as
using python.

HTH,

-- 
Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Grepping a file for words in a list

2006-02-24 Thread John Purser
On Fri, 24 Feb 2006 12:23:22 -0800 (PST)
Danny Yoo <[EMAIL PROTECTED]> wrote:

> 
> 
> On Fri, 24 Feb 2006, John Purser wrote:
> 
> > I'm writing a system admin script in python that checks recently
> > accessed files for keywords like "failed, denied, error,..." etc.
> > I'm using popen to call grep -F   but it's VERY
> > slow.  Can anyone suggest a faster method to do this?
> 
> Hi John,
> 
> This should not be slow, but we have to see what exactly is going on
> here. Can you give us more details?  How many keywords are you
> typically passing in ?  How large is the ?  What do
> you mean by "slow"?
> 


-- 
What good is an obscenity trial except to popularize literature?
-- Nero Wolfe, "The League of Frightened Men"

Danny, 

"what exactly is going on" is that I'm a doofus who will spend hours
figuring out exactly what command string will get grep to work but then
won't bother to put it in his code!!!

DOH!!!

John
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Grepping a file for words in a list

2006-02-24 Thread Danny Yoo


On Fri, 24 Feb 2006, John Purser wrote:

> I'm writing a system admin script in python that checks recently
> accessed files for keywords like "failed, denied, error,..." etc.  I'm
> using popen to call grep -F   but it's VERY slow.  Can
> anyone suggest a faster method to do this?

Hi John,

This should not be slow, but we have to see what exactly is going on here.
Can you give us more details?  How many keywords are you typically passing
in ?  How large is the ?  What do you mean by "slow"?

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Grepping a file for words in a list

2006-02-24 Thread Bob Gailer
John Purser wrote:
> Hello,
>
> I'm writing a system admin script in python that checks recently
> accessed files for keywords like "failed, denied, error,..." etc.  I'm
> using popen to call grep -F   but it's VERY slow.  Can
> anyone suggest a faster method to do this?
>   
I don't know whether it'd be faster, but you could read the file into a 
Python string, then use re to find the keywords. What do you do after 
finding them?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Grepping a file for words in a list

2006-02-24 Thread John Purser
Hello,

I'm writing a system admin script in python that checks recently
accessed files for keywords like "failed, denied, error,..." etc.  I'm
using popen to call grep -F   but it's VERY slow.  Can
anyone suggest a faster method to do this?

Thanks,

John Purser
Ubuntu Linux
Python 2.4

-- 
Courage is your greatest present need.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor