Computer >> Computer tutorials >  >> Programming >> Python

Unix filename pattern matching in Python (fnmatch)


The wildcard patterns used in Unix shell command lines are different from regular expression syntax. In Python’s standard library, fnmatch module provides to Unix wild card patterns.

Following functions are defined in fnmatch module

fnmatch()

This function needs two parameters – file name and string pattern of characters. The file’s name is matched with given pattern and function returns True or False.

Following example lists all files that match the pattern “*.py” from the current directory.

>>> import glob,fnmatch
>>> for files in (glob.glob('*.*')):
if (fnmatch.fnmatch(files, "*.py")):
print (files)

fnmatchcase()

This function is similar to fnmatch(). However this function performs case sensitive matching of patters with file name.

In following example, all files with name starting with ‘l’ and ‘.py’ extension are first collected in a files list object. The fnmatch() function is applied to print only those names starting with ‘lo’ characters.

>>> files=glob.glob("l*.*")
>>> files
['LICENSE.txt', 'lines.txt', 'listwidget.ui', 'lo1.ui', 'lo2.ui', 'lo3.ui', 'logo.png', 'logo.svg', 'lw.ui']
>>> for file in files:
if fnmatch.fnmatchcase(file,"lo*.*")==True:
print (file)
lo1.ui
lo2.ui
lo3.ui
logo.png
logo.svg

filter()

This function returns only those files whose name matches with given pattern parameter.

Following statement returns list of files with ‘.txt’ extension out of all files in current directory.

>>> fnmatch.filter(files,"*.txt")
['a!.txt', 'data().txt', 'dict.txt', 'json.txt', 'LICENSE.txt', 'lines.txt', 'msg.txt', 'NEWS.txt', 'test.txt', 'zen.txt', 'zen1.txt', 'zenbak.txt']

translate()

This function is useful to convert UNIX style pattern into corresponding RegEx form. The return value of translate() function should be converted in regular expression by using compile() function of re module. This is then used to match pattern.

>>> pattern="*.txt"
>>> import re
>>> reg=fnmatch.translate(pattern)
>>> reg
'(?s:.*\\.txt)\\Z'
>>> rec=re.compile(reg)
>>> for file in glob.glob("*.*"):
if re.match(rec,file):
print (file)

In this article functions in fnmatch module have been explained.