{"id":27973,"date":"2023-09-24T14:23:39","date_gmt":"2023-09-24T08:53:39","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=27973"},"modified":"2023-09-24T14:23:50","modified_gmt":"2023-09-24T08:53:50","slug":"python-fnmatch-library","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/","title":{"rendered":"Why Python Fnmatch Library is a Game-Changer for File Management"},"content":{"rendered":"\n<p>Python provides us with an easy way to deal with Unix shell-style wildcards by giving us access to the fnmatch library. It is utilized for multiple things like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Filtering filenames,<\/li>\n\n\n\n<li>Searching for files, and <\/li>\n\n\n\n<li>Validating <a href=\"http:\/\/www.pythonpool.com\/python-user-input\/\" target=\"_blank\" rel=\"noopener\">user input<\/a>.<\/li>\n<\/ul>\n\n\n\n<p>The output is always in the form of boolean TRUE or FALSE.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#Wildcards_used_with_fnmatch_library\" >Wildcards used with fnmatch library<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#Commonly_used_fnmatch_functions\" >Commonly used fnmatch functions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#Usage_of_fnmatchfnmatch\" >Usage of fnmatch.fnmatch()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#Usage_of_fnmatchfnmatchcase\" >Usage of fnmatch.fnmatchcase()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#Usage_of_fnmatchfilter\" >Usage of fnmatch.filter()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#Usage_of_fnmatchtranslate\" >Usage of fnmatch.translate()<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#fnmatch_for_files_searching\" >fnmatch for files&#8217; searching<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#fnmatch_for_validating_user_input\" >fnmatch for validating user input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#python_fnmatch_regex\" >python fnmatch regex<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#python_fnmatch_exclude_pattern\" >python fnmatch exclude pattern<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#python_fnmatch_vs_glob_python\" >python fnmatch vs glob python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#Ignore_case_in_python_fnmatch\" >Ignore case in python fnmatch<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#python_fnmatch_digits\" >python fnmatch digits<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#python_fnmatch_multiple_patterns\" >python fnmatch multiple patterns<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#FAQS\" >FAQS<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#Trending_Python_Articles\" >Trending Python Articles<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-wildcards-used-with-fnmatch-library\"><span class=\"ez-toc-section\" id=\"Wildcards_used_with_fnmatch_library\"><\/span>Wildcards used with fnmatch library<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"940\" height=\"788\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2023\/09\/Copy-of-web-crawling.png\" alt=\"\" class=\"wp-image-28020\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2023\/09\/Copy-of-web-crawling.png 940w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2023\/09\/Copy-of-web-crawling-300x251.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2023\/09\/Copy-of-web-crawling-768x644.png 768w\" sizes=\"(max-width: 940px) 100vw, 940px\" \/><figcaption class=\"wp-element-caption\"><em>python fnmatch<\/em><\/figcaption><\/figure>\n\n\n\n<p>These are some of the widely used wildcards used with fnmatch library.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\"><strong>WILDCARDS<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>USE<\/strong><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">?<\/td><td class=\"has-text-align-center\" data-align=\"center\">any single character.<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">*<\/td><td class=\"has-text-align-center\" data-align=\"center\">any sequence of characters<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">[sequence]<\/td><td class=\"has-text-align-center\" data-align=\"center\">any character in the sequence<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">[!sequence]<\/td><td class=\"has-text-align-center\" data-align=\"center\">any character not in the sequence&nbsp;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-commonly-used-fnmatch-functions\"><span class=\"ez-toc-section\" id=\"Commonly_used_fnmatch_functions\"><\/span>Commonly used fnmatch functions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The fnmatch module has these four primary functions:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>fnmatch.fnmatch(filename, pattern)<\/strong><\/li>\n\n\n\n<li><strong>fnmatch.fnmatchcase(filename, pattern)<\/strong><\/li>\n\n\n\n<li><strong>fnmatch.filter(names, pattern)<\/strong><\/li>\n\n\n\n<li><strong>fnmatch.translate(pattern)<\/strong><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-usage-of-fnmatch-fnmatch\"><span class=\"ez-toc-section\" id=\"Usage_of_fnmatchfnmatch\"><\/span>Usage of <strong>fnmatch.fnmatch<\/strong>()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>It is used to match filenames with the given pattern. The case of letters is dependent on the operating system. Usually, it is changed to similar case for os that is not case sensitive. In this example, you can obtain all file names ending with .txt i.e. a;; text files.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport fnmatch\nfilenames = &#x5B;&quot;abc.txt&quot;, &quot;ddd.py&quot;, &quot;c.txt&quot;]\nans = &#x5B;i for i in filenames if fnmatch.fnmatch(i, &quot;*.txt&quot;)]\nprint(ans)\n<\/pre><\/div>\n\n\n<p>Let&#8217;s have a look at the output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;'abc.txt', 'c.txt']\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-usage-of-fnmatch-fnmatchcase\"><span class=\"ez-toc-section\" id=\"Usage_of_fnmatchfnmatchcase\"><\/span>Usage of <strong>fnmatch.fnmatchcase<\/strong>()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This fnmatch extension is case sensitive. In this example, you can try matching <a href=\"http:\/\/www.pythonpool.com\/numpy-read-csv\/\" target=\"_blank\" rel=\"noopener\">csv files<\/a> files starting with &#8220;data&#8221;. Say, after executing this, you may get output as: data.csv, data1.csv , data12.csv and so on.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport fnmatch\nimport os\npattern = 'data*.csv'  \nprint(f'Pattern: {pattern}\\n')\nfiles = os.listdir('.')\nfor i in files:\n    match = fnmatch.fnmatchcase(i, pattern)\n    print(f'Filename: {i:&lt;25} Match: {match}')\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-usage-of-fnmatch-filter\"><span class=\"ez-toc-section\" id=\"Usage_of_fnmatchfilter\"><\/span>Usage of <strong>fnmatch.filter<\/strong>()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>It returns a list of files altogether that match the given pattern. There is no need of re-iterating over the files one by one. This example provides a pattern to match files with a .jpg extension.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport fnmatch\nimport os\npattern = '*.jpg'  \nfiles = os.listdir('.')\nmatches = fnmatch.filter(files, pattern)\nprint(f'Filenames matching the pattern: {matches}')\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-usage-of-fnmatch-translate\"><span class=\"ez-toc-section\" id=\"Usage_of_fnmatchtranslate\"><\/span>Usage of <strong>fnmatch.<strong>translate<\/strong><\/strong>()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This converts a pattern into a regular expression using translate() function which can be used for file matching too. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport fnmatch\nimport os\nimport re\n\npattern = 'file*.txt'\nregexp = fnmatch.translate(pattern)\n#to compile\nreobj = re.compile(regex_pattern)\n\nfiles = os.listdir('.')\n# regex for file matching\nans = &#x5B;i for i in files if reobj.match(i)]\nprint(f'Translated Pattern : {regexp}')\nprint(f'Matched Files: {ans}')\n<\/pre><\/div>\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-juliett monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-image\"><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_.webp 1200w \" alt=\"[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date\" \/><\/a><\/div><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Trending<\/span><span class=\"monsterinsights-inline-popular-posts-border\" ><\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\">[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-fnmatch-for-files-searching\"><span class=\"ez-toc-section\" id=\"fnmatch_for_files_searching\"><\/span>fnmatch for files&#8217; searching<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can use fnmatch to search for specific files. This example prints a list of all files in the <a href=\"http:\/\/www.pythonpool.com\/python-get-current-directory\/\" target=\"_blank\" rel=\"noopener\">current directory<\/a> that start with the letter a:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport fnmatch\nimport os\ncwd = os.getcwd()\n\n#to store matched files\nans = &#x5B;]                    \nfor i in os.listdir(cwd):\n    if fnmatch.fnmatch(i, &quot;a*&quot;):\n        ans.append(i)\nprint(ans)\n<\/pre><\/div>\n\n\n<p>And the output will be:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;'abc.txt']\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-fnmatch-for-validating-user-input\"><span class=\"ez-toc-section\" id=\"fnmatch_for_validating_user_input\"><\/span>fnmatch for validating user input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If you wish to validate user input, you may use fnmatch. For example, we can specify a file extension and check if a user input ends with a particular extension.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport fnmatch\ndef Validation(file):\n    if not fnmatch.fnmatch(file, &quot;*.exe&quot;):\n        raise ValueError(&quot;filename must end with the .exe extension.&quot;)\n\n# user input \nf = input(&quot;Enter a filename: &quot;)\n\n# for Validation\nValidation(f)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-fnmatch-regex\"><span class=\"ez-toc-section\" id=\"python_fnmatch_regex\"><\/span>python fnmatch regex<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Apart from the aforementioned examples, you can use regex in multiple ways for pattern matching for files. You may use any combination of numbers or wildcard characters while using the translate function of fnmatch library.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nregexp = re.compile('_(&#x5B;0-9-_.]+\\...)')\nfiles=&#x5B;]\nfor i in os.listdir(cwd):\n    if fnmatch.fnmatch(i, regexp):\n        files.append(i)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-fnmatch-exclude-pattern\"><span class=\"ez-toc-section\" id=\"python_fnmatch_exclude_pattern\"><\/span>python fnmatch exclude pattern<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can use the if not phrase with the pattern in order to exclude a part of it. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport fnmatch\nfiles = &#x5B;&quot;file1.txt&quot;, &quot;file2.jpg&quot;, &quot;file3.txt&quot;, &quot;file4.doc&quot;]\n# Pattern to exclude\nexclude_pattern = &quot;*.txt&quot;\n# Use fnmatch.filter to exclude files \nAns = &#x5B;i for i in files if not fnmatch.fnmatch(i, exclude_pattern)]\nprint(Ans)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-fnmatch-vs-glob-python\"><span class=\"ez-toc-section\" id=\"python_fnmatch_vs_glob_python\"><\/span>python fnmatch vs glob python <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python&#8217;s glob module can perform more complex pattern matching as compared to python fnmatch. So, fnmatch works for single strings well but glob can execute pattern matching in directories too.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\nimport glob\ndef func(dirname, extensions):\n    #i as filename\n    for i in glob.glob(dirname):\n        base, ext = os.path.splitext(i)\n        if ext.lower() in extensions:\n            print i\nfunc('dir', &#x5B;'.jpeg', '.png', '.jpg'])\n<\/pre><\/div>\n\n\n<p>In this example, first we need to pass the directory name as parameter to the func function. It will check all the files in given directory. <code>glob.glob()<\/code> has been used for the same. <code>os.path.splitext()<\/code> checks file entensions and the ones that are matched are printed.<\/p>\n\n\n\n<p>In addition to this, fnmatch usually has boolean values as output unless we are looping all the files and after matching, appending them to a list. This displays the files that have matched. However, with glob we directly obtain a list of files.<\/p>\n\n\n\n<p>Let&#8217;s have a look at these differences in a consolidated format:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>fnmatch<\/th><th>glob<\/th><\/tr><\/thead><tbody><tr><td>It can do easy pattern matching<\/td><td>It can handle complex pattern matching<\/td><\/tr><tr><td>It can&#8217;t do pattern matching in directories<\/td><td>It can do pattern matching in directories<\/td><\/tr><tr><td>It is simple<\/td><td>It is quite complex<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-golf monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Popular now<\/span><span class=\"monsterinsights-inline-popular-posts-border\" ><\/span><span class=\"monsterinsights-inline-popular-posts-border-2\" ><\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\">[Fixed] nameerror: name Unicode is not defined<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-ignore-case-in-python-fnmatch\"><span class=\"ez-toc-section\" id=\"Ignore_case_in_python_fnmatch\"><\/span>Ignore case in python fnmatch<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For ignoring the case, one can use the filter extension of fnmatch library. In the given piece of code, all the .py files i.e. files with python code will be selected. Filter is an extremely important extension of python fnmatch module.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\nfrom fnmatch import filter\nfilter(os.listdir('.'), '*.&#x5B;Pp]&#x5B;Yy]')\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-fnmatch-digits\"><span class=\"ez-toc-section\" id=\"python_fnmatch_digits\"><\/span>python fnmatch digits <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>One can make use of python fnmatch library to match digits in a file too. One can execute this in two ways: either digits + some characters with the file name or a filename with digits only. An example of the first one can be:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npattern = 'abc_def_&#x5B;0-9]*.json'\n<\/pre><\/div>\n\n\n<p>For the 2nd case, you can specify the name of directory with the exact location and specify the pattern which comprises of digits from 0 to 9.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport fnmatch\nimport os\n\ndir = 'dirname' \npattern = '*&#x5B;0-9]*'  \n\n# List all files in the specified directory\nfiles = os.listdir(dir)\n\n# Filter files that match the pattern\nres = &#x5B;i for i in files if fnmatch.fnmatch(i, pattern)]\n\n# Print the matching filenames\nfor j in res:\n    print(j)\n#i, j refer to filenames\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-fnmatch-multiple-patterns\"><span class=\"ez-toc-section\" id=\"python_fnmatch_multiple_patterns\"><\/span>python fnmatch multiple patterns<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can match multiple patterns using fnmatch &#8216;s filter extension. You can specify a set of patterns in order to match them.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport fnmatch\n#p for patterns\np = &#x5B;&quot;*.py&quot;, &quot;*.txt&quot;]\npath= &#x5B;&quot;src\/main.py&quot;, &quot;src\/README.md&quot;]\n\nAns = fnmatch.filter(path, p)\n# output will be :\n# &#x5B;&quot;src\/main.py&quot;]\n<\/pre><\/div>\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-alpha monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Trending<\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\">[Solved] runtimeerror: cuda error: invalid device ordinal<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-faqs\"><span class=\"ez-toc-section\" id=\"FAQS\"><\/span>FAQS <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1695038388883\"><strong class=\"schema-faq-question\">What is the difference between fnmatch.fnmatch() and fnmatch.filter()?<\/strong> <p class=\"schema-faq-answer\">The first one deals with one file at a time whereas the filter function displays a list of files that match the pattern.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1695038391226\"><strong class=\"schema-faq-question\">Which special characters can be used with fnmatch library?<\/strong> <p class=\"schema-faq-answer\">Special characters like ? (Question Mark), <code>*<\/code> (Asterisk), <code>[]<\/code> (Square Brackets), <code>{}<\/code> (Curly Braces),  <code>[!]<\/code> or <code>[^]<\/code> (Exclamation Mark or Caret), etc can be used.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1695103694404\"><strong class=\"schema-faq-question\">Can you use fnmatch with recursive matching?<\/strong> <p class=\"schema-faq-answer\">No, fnmatch doesn&#8217;t support recursive matching.<\/p> <\/div> <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Through this article, you must have comprehended the usage of Python fnmatch in detail. It covers four important functions of fnmatch along with their applications in Python. Apart from this, this blog also tells us how we can differentiate between Python fnmatch and the widely used glob module.<\/p>\n\n\n<div class=\"monsterinsights-widget-popular-posts monsterinsights-widget-popular-posts-delta monsterinsights-popular-posts-styled monsterinsights-widget-popular-posts-columns-2\"><h2 class=\"monsterinsights-widget-popular-posts-widget-title\"><span class=\"ez-toc-section\" id=\"Trending_Python_Articles\"><\/span>Trending Python Articles<span class=\"ez-toc-section-end\"><\/span><\/h2><ul class=\"monsterinsights-widget-popular-posts-list\"><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_.webp 1200w \" alt=\"[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 11, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined.webp 1200w \" alt=\"[Fixed] nameerror: name Unicode is not defined\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] nameerror: name Unicode is not defined<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal.webp 1200w \" alt=\"[Solved] runtimeerror: cuda error: invalid device ordinal\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Solved] runtimeerror: cuda error: invalid device ordinal<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-type-numpy-ndarray-doesnt-define-__round__-method\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method.webp 1200w \" alt=\"[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><\/ul><\/div><p><\/p>","protected":false},"excerpt":{"rendered":"<p>Python provides us with an easy way to deal with Unix shell-style wildcards by giving us access to the fnmatch library. It is utilized for &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Why Python Fnmatch Library is a Game-Changer for File Management\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#more-27973\" aria-label=\"More on Why Python Fnmatch Library is a Game-Changer for File Management\">Read more<\/a><\/p>\n","protected":false},"author":38,"featured_media":28230,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4345],"tags":[5829,5825,5827,5826,5828],"class_list":["post-27973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-module","tag-python-fnmatch-recursive","tag-python-fnmatch-regex","tag-python-fnmatch-vs-glob","tag-python-fnmatch-wildcard","tag-python-fnmatchcase","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Why Python Fnmatch Library is a Game-Changer for File Management<\/title>\n<meta name=\"description\" content=\"Python provides us with an easy way to deal with Unix shell-style wildcards by giving us access to the fnmatch library\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Why Python Fnmatch Library is a Game-Changer for File Management\" \/>\n<meta property=\"og:description\" content=\"Python provides us with an easy way to deal with Unix shell-style wildcards by giving us access to the fnmatch library. It is utilized for multiple things\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-24T08:53:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-24T08:53:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2023\/09\/Python-Fnmatch-library.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Namrata Gulati\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Namrata Gulati\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/\"},\"author\":{\"name\":\"Namrata Gulati\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/294338f378f0853e6af4ca4a5a907ea6\"},\"headline\":\"Why Python Fnmatch Library is a Game-Changer for File Management\",\"datePublished\":\"2023-09-24T08:53:39+00:00\",\"dateModified\":\"2023-09-24T08:53:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/\"},\"wordCount\":861,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2023\/09\/Python-Fnmatch-library.webp\",\"keywords\":[\"python fnmatch recursive\",\"python fnmatch regex\",\"python fnmatch vs glob\",\"python fnmatch wildcard\",\"python fnmatchcase\"],\"articleSection\":[\"Module\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/\",\"name\":\"Why Python Fnmatch Library is a Game-Changer for File Management\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2023\/09\/Python-Fnmatch-library.webp\",\"datePublished\":\"2023-09-24T08:53:39+00:00\",\"dateModified\":\"2023-09-24T08:53:50+00:00\",\"description\":\"Python provides us with an easy way to deal with Unix shell-style wildcards by giving us access to the fnmatch library\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695038388883\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695038391226\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695103694404\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2023\/09\/Python-Fnmatch-library.webp\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2023\/09\/Python-Fnmatch-library.webp\",\"width\":1200,\"height\":628,\"caption\":\"Python Fnmatch library\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Why Python Fnmatch Library is a Game-Changer for File Management\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/294338f378f0853e6af4ca4a5a907ea6\",\"name\":\"Namrata Gulati\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/19c5e6bfbc6202d4017b79f726b2ad5e520491d67ff428a87c071afef23ecd89?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/19c5e6bfbc6202d4017b79f726b2ad5e520491d67ff428a87c071afef23ecd89?s=96&d=wavatar&r=g\",\"caption\":\"Namrata Gulati\"}},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695038388883\",\"position\":1,\"url\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695038388883\",\"name\":\"What is the difference between fnmatch.fnmatch() and fnmatch.filter()?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"The first one deals with one file at a time whereas the filter function displays a list of files that match the pattern.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695038391226\",\"position\":2,\"url\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695038391226\",\"name\":\"Which special characters can be used with fnmatch library?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Special characters like ? (Question Mark), * (Asterisk), [] (Square Brackets), {} (Curly Braces),  [!] or [^] (Exclamation Mark or Caret), etc can be used.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695103694404\",\"position\":3,\"url\":\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695103694404\",\"name\":\"Can you use fnmatch with recursive matching?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"No, fnmatch doesn't support recursive matching.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Why Python Fnmatch Library is a Game-Changer for File Management","description":"Python provides us with an easy way to deal with Unix shell-style wildcards by giving us access to the fnmatch library","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/","og_locale":"en_US","og_type":"article","og_title":"Why Python Fnmatch Library is a Game-Changer for File Management","og_description":"Python provides us with an easy way to deal with Unix shell-style wildcards by giving us access to the fnmatch library. It is utilized for multiple things","og_url":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/","og_site_name":"Python Pool","article_published_time":"2023-09-24T08:53:39+00:00","article_modified_time":"2023-09-24T08:53:50+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2023\/09\/Python-Fnmatch-library.webp","type":"image\/webp"}],"author":"Namrata Gulati","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Namrata Gulati","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/"},"author":{"name":"Namrata Gulati","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/294338f378f0853e6af4ca4a5a907ea6"},"headline":"Why Python Fnmatch Library is a Game-Changer for File Management","datePublished":"2023-09-24T08:53:39+00:00","dateModified":"2023-09-24T08:53:50+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/"},"wordCount":861,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2023\/09\/Python-Fnmatch-library.webp","keywords":["python fnmatch recursive","python fnmatch regex","python fnmatch vs glob","python fnmatch wildcard","python fnmatchcase"],"articleSection":["Module"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-fnmatch-library\/#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/","url":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/","name":"Why Python Fnmatch Library is a Game-Changer for File Management","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2023\/09\/Python-Fnmatch-library.webp","datePublished":"2023-09-24T08:53:39+00:00","dateModified":"2023-09-24T08:53:50+00:00","description":"Python provides us with an easy way to deal with Unix shell-style wildcards by giving us access to the fnmatch library","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695038388883"},{"@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695038391226"},{"@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695103694404"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-fnmatch-library\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2023\/09\/Python-Fnmatch-library.webp","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2023\/09\/Python-Fnmatch-library.webp","width":1200,"height":628,"caption":"Python Fnmatch library"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Why Python Fnmatch Library is a Game-Changer for File Management"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/294338f378f0853e6af4ca4a5a907ea6","name":"Namrata Gulati","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/19c5e6bfbc6202d4017b79f726b2ad5e520491d67ff428a87c071afef23ecd89?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/19c5e6bfbc6202d4017b79f726b2ad5e520491d67ff428a87c071afef23ecd89?s=96&d=wavatar&r=g","caption":"Namrata Gulati"}},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695038388883","position":1,"url":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695038388883","name":"What is the difference between fnmatch.fnmatch() and fnmatch.filter()?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"The first one deals with one file at a time whereas the filter function displays a list of files that match the pattern.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695038391226","position":2,"url":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695038391226","name":"Which special characters can be used with fnmatch library?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Special characters like ? (Question Mark), * (Asterisk), [] (Square Brackets), {} (Curly Braces),  [!] or [^] (Exclamation Mark or Caret), etc can be used.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695103694404","position":3,"url":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#faq-question-1695103694404","name":"Can you use fnmatch with recursive matching?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"No, fnmatch doesn't support recursive matching.","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/27973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=27973"}],"version-history":[{"count":62,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/27973\/revisions"}],"predecessor-version":[{"id":28235,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/27973\/revisions\/28235"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/28230"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=27973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=27973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=27973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}