This module is used for Unix shell style wildcard matching. fnmatch () compares one filename against a pattern and returns TRUE if they match, otherwise it returns FALSE.
Comparison is case sensitive when the operating system uses a case sensitive file system.
Special characters and their functions used in shell-style wildcards:
Metacharacters must be enclosed in square brackets to literally match. For example, "[?]" Matches the character "?".
Functions provided by the fnmatch module
Example: script to search all files starting with & # 39; fnmatch & # 39 ; and ending with & # 39; .py & # 39;
$ python fnmatch_fnmatch.py Pattern: fnmatch _ *. py Filename: __init__.py False Filename: fnmatch_filter .py True Filename: fnmatch_fnmatch.py True Filename: fnmatch_fnmatchcase.py True Filename: fnmatch_translate.py True Filename: index.rst False
Example: script for case sensitive comparison, regardless of settings file system and opera system.
$ python fnmatch_fnmatchcase.py Pattern: FNMATCH _ *. PY Filename: __init__.py False Filename: fnmatch_filter.py False Filename: FNMATCH_FNMATCH.PY True Filename: fnmatch_fnmatchcase.py False_name Filerse.fnatem / pre>
Example. Filter files by multiple extensions.
$ python fnmatch_filter.py Pattern: fnmatch _ *. py Files: [ '__init __. py',' fnmatch_filter.py', 'fnmatch_fnmatch.py',' fnmatch_fnmatchcase.py', 'fnmatch_translate.py',' index.rstatch'] Matches: ['fnmatch_filter.py_fnmatch , 'fnmatch_fnmatchcase.py',' fnmatch_translate.py']
'(? S:. * . Txt) Z' _sre.SRE_Match object; span = (0, 10), match = 'foobar.txt'
This article is courtesy of Aditi Gupta . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting the article [email protected] ... See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you'd like to share more information on the topic discussed above.
I have developed this book to investigate Mesos-based cluster development and integration. I found that data center operating system (DCOS; and it’s command-line interface [CLI]) was a natural progr...
A Gentle Introduction to Numerical Simulations with Python 3.6. Computing, in the sense of doing mathematical calculations, is a skill that mankind has developed over thousands of years. Programmin...
Shabbir Challawala has over 8 years of rich experience in providing solutions based on MySQL and PHP technologies. He is currently working with KNOWARTH Technologies. He has worked in various PHP-base...
For many decades, some powerful trends have been in place. Computer hardware has rap- idly been getting faster, cheaper and smaller. Internet bandwidth (that is, its information carrying capacity) has...