[Tutor] Reading and Writing
Erik Price
erikprice@mac.com
Fri, 30 Aug 2002 08:13:20 -0400
On Friday, August 30, 2002, at 06:54 AM, Ibraheem Umaru-Mohammed wrote:
> # match a filename that matches the following regular expression:
> # \d = a single digit [0-9]
> # \w+ = at least one word character [a-zA-Z]
> # \d = a single digit [0-9]
> # .htm = a literal
> pattern = "\d\w+\d.htm"
> for filename in os.listdir(html_directory):
> # if we have a match, print the matching string.
> m = re.match(pattern, filename)
> if m: print repr(m.group(0))
Oh, I see. You don't have to invoke "glob" at all, you can just do a
string test. Great! Thanks, Ibz.
(BTW I think that you need to escape your literal dot, otherwise it
gets parsed as "any character" -- but then this is just an example so
who cares.)
Since I was just learning about list comprehensions in another thread,
I think I see where one can be used here:
<code>
pattern = "\d\w+\d\.htm"
matches = [ m for filename in os.listdir(html_directory) if
re.match(pattern, filename) ]
for m in matches: print repr(m.group(0))
</code>
But I think your code is still better because it doesn't use two
separate for loops (mine has one in the LC and another to do the
printing). It is just a mental exercise.
Erik
[You can't use LCs to do things like "print", right? It just does a
kind of elaborate list assignment if I understand correctly.]
--
Erik Price
email: erikprice@mac.com
jabber: erikprice@jabber.org