easier solution possible to find a stringpattern in a list?

Gerrit Holl gerrit at nl.linux.org
Sat Jan 4 08:19:25 EST 2003


christof hoeke schreef op zaterdag  4 januari om 14:07:29 +0000:
> i have a list of filenames and want to know if there is one or more files
> with a given pattern in it.
> a bit similar to what glob.glob(pattern) is doing.

> is there already a function in the standard modules that can do something
> like this?

You are looking for module fnmatch:
>>> files = ["a.txt", "b.text", "c.txt", "txtfile.bz2"]
>>> import fnmatch
>>> fnmatch.filter(files, "*.txt")
['a.txt', 'c.txt']
>>> print "%d times in the list" % len(_)
2 times in the list

More info:
http://www.python.org/doc/2.3a1/lib/module-fnmatch.html

yours,
Gerrit.

-- 
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/





More information about the Python-list mailing list