On Wed, Dec 24, 2008 at 01:12:55AM -0000, Alan Gauld wrote:
> 
> "Kent Johnson" <ken...@tds.net> wrote
> 
> >>       for filename in os.listdir(directory):
> >>           result = re.match(s, filename)
> >>   print result
> >
> >You never open and read the files. You are searching for the pattern
> >in the filename, not in the contents of the file.
> 
> Also note that match() only searches starting at the start of the 
> string.
> 
> Thus match will find foo at
> 
> foobar
> 
> but not in
> 
> sofoo
> 
> You usually need to use search()  to find the pattern anywhere
> within the string.
> 
> Also look at the thread earlier this week on using listdir() and
> the fileinput module.

Hello again and thanks for the encouragement. 

I have been working on this problem again today and switched to the fileinput 
method. What I can't figure out now is how to pass a compiled regex to an 
optparse option. I'm confused ias to "option" versus "arg" when using the 
optparse module. In fact there seems to be no way to define what the arg should 
be; only options. Is the arg always implied? I have read several pages on 
optparse and am none the wiser.

How do I fix the rx = re.compile('-x') line below so that the string I pass on 
the command line gets passed into the re.compile?

#!/usr/bin/python
import fileinput, sys, string, optparse, re

#def main():
optparser = optparse.OptionParser()
optparser.add_option("-x", "--regx", help="regular expression")
# take the first argument out of sys.argv and assign it to searchterm
#searchterm, sys.argv[1:] = sys.argv[1], sys.argv[2:]
(options, args) = optparser.parse_args()

rx = re.compile('-x')

for line in fileinput.input():
    num_matches = string.count(line, rx)
if num_matches:
    print "found '%s' %d times in %s on line %d." % (rx, num_matches,
        fileinput.filename(), fileinput.filelineno())

#if __name__ == '__main__':
    #main()

> HTH,
> 
> 
> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
I fear you speak upon the rack,
Where men enforced do speak anything.

- William Shakespeare
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to