+

Unix filename pattern matching in Python

Here we will see how we can get UNIX shell style pattern matching methods using Python. There is a fnmatch module that is used to get the job done. This module is used to match a filename against a pattern and then return True or False according to the matches.

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

 import fnmatch 

There are several wildcard characters in the Unix terminal that match patterns. It`s like below &





  • & # 8216; * & # 8217; The asterisk is used to match everything.
  • & # 8216;? & # 8217; The question mark is designed to match a single character.
  • [seq] Sequences are used to match characters in a sequence
  • [! seq] Not in Sequence are used to match characters that are not in the sequence.
  • & # 8216; * & # 8217; The asterisk is used to match everything.
  • & # 8216;? & # 8217; The question mark is designed to match a single character.
  • [seq] Sequences are used to match characters in a sequence
  • [! seq] Not in Sequence are used to match characters that are not in the sequence.
 import fnmatch import os file_pattern = `test_f *` files = os.listdir (`. / unix_files`) for filename in files: print (` File: {}: {} `.format (filename, fnmatch.fnmatch (filename, file_pattern))) 

If we want to find an asterisk or a question mark as a character, we must use them like this: [*] or [?]

 $ 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_file 3 .txt: True $ 

The fnmatch () method takes two arguments, the filename and the pattern. This function is used to check whether the file name matches the given pattern or not. If the operating system is case sensitive, the parameters are normalized to upper or lower case before matching.

Sample 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 (` Matched files: `+ str (match_file)) 
 $ python3 310.UNIX_filename.py All files: [`test_file5.txt`,` test_file2.png`, ` test_file1.txt`, `a nother_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 filter () method also takes two parameters. The first &these are the names, and the second &sample. This pattern finds a list of matching filenames from a list of all filenames.

Sample Code

 import fnmatch, re file_pattern = ` test_f * .txt` unix_regex = fnmatch.translate (file_pa ttern) regex_object = re.compile (unix_regex) print (`Regular Expression:` + str (unix_regex)) print (`Match Object:` + str (regex_object.match (` test_file_abcd123.txt`))) 
 $ python3 310.UNIX_filename.py Regular Expression :(? s: test_f. *. txt) Match Object : & lt; _sre.SRE_Match object; span = (0, 21), match = `test_file_abcd123.txt` & gt; $ 



Get Solution for free from DataCamp guru