[Tutor] Line matching without a for statement?

Kent Johnson kent_johnson at skillsoft.com
Thu Oct 28 05:53:20 CEST 2004


If you use
output = os.popen(...).read()
I think you will get the entire output in one string. Then compile your re with
interface = re.compile(r'^eth\d', re.MULTILINE)
so the ^ will match the beginning of any line in the string. Then I think 
the search() will do what you want.

Alternatively just use a or loop, it's not such a big deal! Be happy you 
aren't writing it in Java :-)
for line in output:
   if interface.search(line):
     print "found"
     break  # if you only want to print "found" once

Kent

At 10:35 PM 10/27/2004 -0400, Tom Tucker wrote:
>Tutors,
>
>Good evening.  Is it possible to search/query a variable without doing
>a for statement (ie. for line in ouptut).  Hopefully my terminology is
>correct.  The below example script might make more sense.  Thoughts?
>
>Thanks,
>Tom
>
>SCRIPT (Example)
>######################
>#!/usr/bin/python
>import os, re
>
>output = os.popen('/sbin/ifconfig -a').readlines()
>interface = re.compile(r'^eth\d')
>
>if interface.search(output):
>         print "found an interface"
>
>
>ERROR OUTPUT
>###########################
>Traceback (most recent call last):
>File "./test.py", line 9, in ?
>if interface.search(output):
>TypeError: expected string or buffer
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list