[Tutor] returning the entire line when regex matches

Alan Gauld alan.gauld at btinternet.com
Mon May 4 19:45:17 CEST 2009

"Nick Burgess" <burgess.nick at gmail.com> wrote 
> for line in log:
>    if not re.search(r'notneeded.com|notneeded1.com',line):
>        outFile.write(line)
> I tried the in method but it missed any other strings I put in, like
> the pipe has no effect.  More complex strings will likely be needed so
> perhaps re might be better..?

Yes, in only works for simple strings. If you need combinations 
then the regex is better

> I have been playing with os.walk but im not sure if it is the best way.

It is almost certainly the best way.

> I guess merging all of the files into one big one before the parse
> would work but I would need help with that too.

You shouldn't need to do that. Your function can take a file and process 
it so just use os.walk to feed it files one by one as you find them

If the file names vary you might find glob.glob useful too.

I show examples of using os,.walk and glob in the OS topic 
in my tutorial. Look under the heading 'Manipulating Files'


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list