# Unix filename pattern matching in Python

PythonProgrammingServer Side Programming

Here we will see how we can get the UNIX shell style pattern matching techniques using Python. There is a module called fnmatch, which is used to do the work. This module is used to compare file name against a pattern, then returns True or False according to the matches.

To use it at first we need to import it the fnmatch standard library module.

import fnmatch

In the Unix terminal, there are some wildcards to match the patterns. These are like below −

• ‘*’ The asterisk is used to match everything.
• ‘?’ Question Mark is for matching a single character.
• [seq] Sequences are used to match characters in sequence
• [!seq] Not in Sequence are used to match characters which are not present in the sequence.

If we want to search asterisk or question marks as character, then we have to use them like this: [*] or [?]

## The fnmatch() method

The fnmatch() method takes two arguments, these are filename and pattern. This function is used to check whether the filename is matched with the given pattern or not. When the Operating System is case sensitive, then the parameters will be normalized to uppercase or lowercase letter before matching.

## Example Code

import fnmatch
import os
file_pattern = 'test_f*'
files = os.listdir('./unix_files')
for filename in files:
print('File: {}\t: {}'.format(filename, fnmatch.fnmatch(filename, file_pattern)))

## Output

$python3 310.UNIX_filename.py File: test_file5.txt : True File: test_file2.png : True File: test_file1.txt : True File: another_file.txt : False File: TEST_FILE4.txt : False File: abc.txt : False File: test_file3.txt : True$

## The filter() method

The filter() method also takes two parameters. The first one is the names, and the second one is the pattern. This pattern finds the list of matched filenames from the list of all filenames.

## Example Code

import fnmatch
import os
file_pattern = 'test_f*'
files = os.listdir('./unix_files')
match_file = fnmatch.filter(files, file_pattern)
print('All files:' + str(files))
print('\nMatched files:' + str(match_file))

## Output

$python3 310.UNIX_filename.py All files:['test_file5.txt', 'test_file2.png', 'test_file1.txt', 'another_file.txt', 'TEST_FILE4.txt', 'abc.txt', 'test_file3.txt'] Matched files:['test_file5.txt', 'test_file2.png', 'test_file1.txt', 'test_file3.txt']$

## The translate() method

The translate() method takes one parameter. The parameter is a pattern. We can use this function to convert a shell style pattern to another type of pattern to match using regular expressions in Python.

## Example Code

import fnmatch, re
file_pattern = 'test_f*.txt'
unix_regex = fnmatch.translate(file_pattern)
regex_object = re.compile(unix_regex)
print('Regular Expression:' + str(unix_regex))
print('Match Object:' + str(regex_object.match('test_file_abcd123.txt')))

## Output

$python3 310.UNIX_filename.py Regular Expression:(?s:test_f.*\.txt)\Z Match Object:<_sre.SRE_Match object; span=(0, 21), match='test_file_abcd123.txt'>$
Published on 06-Nov-2018 16:30:42