{"id":27973,"date":"2023-09-24T14:23:39","date_gmt":"2023-09-24T08:53:39","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=27973"},"modified":"2026-07-13T12:43:21","modified_gmt":"2026-07-13T07:13:21","slug":"python-fnmatch-library","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/","title":{"rendered":"Python fnmatch: Match Filenames with Wildcards"},"content":{"rendered":"<p><strong>Quick answer:<\/strong> The fnmatch module matches strings against shell-style wildcard patterns. Use * for any sequence, ? for one character, and bracket expressions for character classes. fnmatch is useful for a list of names, but it is not a complete recursive path policy, so choose pathlib, glob, or os.walk when directory traversal matters.<\/p>\n<figure class=\"pythonpool-article-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-fnmatch-library.png\" alt=\"Python Pool infographic comparing fnmatch patterns star question mark character classes filter and path handling\" width=\"1536\" height=\"1024\" loading=\"lazy\" decoding=\"async\"><figcaption>fnmatch applies shell-style filename patterns such as *.py and report-??.csv; choose the matching function and path scope deliberately.<\/figcaption><\/figure>\n<p>The Python <code>fnmatch<\/code> library matches strings against Unix shell-style wildcard patterns. It is useful when you already have file names, user-supplied names, or directory entries and want to check whether each one fits a simple pattern. The official <a href=\"https:\/\/docs.python.org\/3\/library\/fnmatch.html\">Python fnmatch documentation<\/a> describes these rules as shell-style wildcards, not full regular expressions.<\/p>\n<p>The key point is that <code>fnmatch<\/code> matches names; it does not walk directories by itself. Use <code>os.listdir()<\/code>, <code>pathlib<\/code>, or <code>glob<\/code> to collect paths, then use <code>fnmatch<\/code> when you need direct pattern checks on those names. For directory basics, see the <a href=\"https:\/\/www.pythonpool.com\/python-get-current-directory\/\">Python current directory guide<\/a>.<\/p>\n<p>The common wildcard characters are <code>*<\/code> for any sequence, <code>?<\/code> for one character, <code>[abc]<\/code> for one allowed character, and <code>[!abc]<\/code> for one excluded character. These patterns are intentionally small and readable. If you need advanced text matching, translate the pattern or use <code>re<\/code> directly.<\/p>\n<p>Use these patterns for file names and simple labels, not for parsing structured text. That keeps the rule obvious to the next reader and avoids turning a small wildcard check into a hidden validation system.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 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\/#Match_A_File_Name_With_fnmatch\" >Match A File Name With fnmatch()<\/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\/#Use_fnmatchcase_For_Exact_Case\" >Use fnmatchcase() For Exact Case<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#Filter_Names_With_fnmatchfilter\" >Filter Names With fnmatch.filter()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#Scan_A_Directory_With_pathlib\" >Scan A Directory With pathlib<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#Translate_A_Pattern_To_Regex\" >Translate A Pattern To Regex<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#Match_Multiple_Patterns\" >Match Multiple Patterns<\/a><\/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_vs_glob\" >fnmatch vs glob<\/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\/#Match_Common_Filename_Patterns\" >Match Common Filename Patterns<\/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\/#Use_Wildcards_And_Character_Classes\" >Use Wildcards And Character Classes<\/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\/#Filter_Lists_And_Control_Case\" >Filter Lists And Control Case<\/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\/#Keep_Path_Boundaries_Explicit\" >Keep Path Boundaries Explicit<\/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\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#What_is_Python_fnmatch_used_for\" >What is Python fnmatch used for?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#What_is_the_difference_between_fnmatch_and_fnmatchcase\" >What is the difference between fnmatch() and fnmatchcase()?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#How_do_I_filter_filenames_with_fnmatch\" >How do I filter filenames with fnmatch?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#Can_fnmatch_match_full_paths\" >Can fnmatch match full paths?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Match_A_File_Name_With_fnmatch\"><\/span>Match A File Name With fnmatch()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>fnmatch.fnmatch()<\/code> returns <code>True<\/code> or <code>False<\/code> for one name and one pattern. On systems with case-insensitive file names, Python may normalize case before comparing. That makes it convenient for everyday file-name checks.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from fnmatch import fnmatch\n\nnames = [\"report.txt\", \"notes.md\", \"data.txt\", \"script.py\"]\ntext_files = [name for name in names if fnmatch(name, \"*.txt\")]\n\nprint(text_files)\n<\/code><\/pre>\n<\/div>\n<p>This is the simplest form for filtering a list that is already in memory. The pattern <code>*.txt<\/code> means any name ending with that extension. The function does not open the files and does not check whether they exist.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Use_fnmatchcase_For_Exact_Case\"><\/span>Use fnmatchcase() For Exact Case<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>fnmatch.fnmatchcase()<\/code> skips operating-system case normalization and compares the pattern exactly as written. Use it when uppercase and lowercase should be treated differently across every platform.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from fnmatch import fnmatchcase\n\nnames = [\"DATA.csv\", \"data.csv\", \"Data.csv\"]\nmatches = [name for name in names if fnmatchcase(name, \"data*.csv\")]\n\nprint(matches)\n<\/code><\/pre>\n<\/div>\n<p>This example only keeps the lowercase name because the pattern starts with lowercase <code>data<\/code>. If your application needs predictable behavior on Windows, macOS, and Linux, choose <code>fnmatchcase()<\/code> when case must be strict.<\/p>\n<p><!-- Python Pool visual layout repair 2026-07-13 --><\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/fnmatch-pattern-b192.png\" alt=\"Python Pool infographic showing a filename, wildcard pattern, fnmatch, and Boolean result\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>fnmatch compares a filename with shell-style wildcard patterns.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Filter_Names_With_fnmatchfilter\"><\/span>Filter Names With fnmatch.filter()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>fnmatch.filter()<\/code> applies one pattern to a list of names and returns the matching names. It is a concise alternative to writing the same list comprehension yourself.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from fnmatch import filter\n\nnames = [\"main.py\", \"README.md\", \"test_api.py\", \"config.toml\"]\npython_files = filter(names, \"*.py\")\n\nprint(python_files)\n<\/code><\/pre>\n<\/div>\n<p>This function is useful for short, direct filters. If you need to apply extra checks, combine patterns, or transform the names while looping, a normal <code>for<\/code> loop or list comprehension is often clearer.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Scan_A_Directory_With_pathlib\"><\/span>Scan A Directory With pathlib<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Because <code>fnmatch<\/code> does not search the filesystem by itself, a common pattern is to list a directory first and then match each path name. The <a href=\"https:\/\/docs.python.org\/3\/library\/pathlib.html\">pathlib documentation<\/a> covers the modern path API used here.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from fnmatch import fnmatch\nfrom pathlib import Path\n\nroot = Path(\".\")\nmatches = [path.name for path in root.iterdir() if fnmatch(path.name, \"test_*.py\")]\n\nprint(matches)\n<\/code><\/pre>\n<\/div>\n<p>This keeps filesystem access separate from pattern matching. Use <code>path.name<\/code> when the pattern should match only the final file name. Use the full path string only when the directory segments are intentionally part of the pattern.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/fnmatch-wildcards-b192.png\" alt=\"Python Pool infographic comparing star, question mark, character class, and filename matches\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Shell-style wildcards express broad, single-character, and character-class matches.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Translate_A_Pattern_To_Regex\"><\/span>Translate A Pattern To Regex<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>fnmatch.translate()<\/code> converts a shell-style pattern into a regular expression string. This is useful when you want to compile the translated pattern once and reuse it many times.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from fnmatch import translate\nimport re\n\npattern = \"data_[0-9][0-9].csv\"\nregex = re.compile(translate(pattern))\n\nprint(bool(regex.match(\"data_24.csv\")))\nprint(bool(regex.match(\"data_final.csv\")))\n<\/code><\/pre>\n<\/div>\n<p>The translated expression is generated for you, so you do not need to hand-write a regular expression for a simple wildcard pattern. If your matching rules are already complex, start with the <code>re<\/code> module instead of forcing them through <code>fnmatch<\/code>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Match_Multiple_Patterns\"><\/span>Match Multiple Patterns<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>fnmatch.filter()<\/code> accepts one pattern at a time. To match multiple patterns, test each name with <code>any()<\/code>. This is clearer than trying to pass a list of patterns into <code>filter()<\/code>.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from fnmatch import fnmatch\n\nnames = [\"main.py\", \"notes.txt\", \"archive.zip\", \"README.md\"]\npatterns = [\"*.py\", \"*.txt\"]\n\nmatches = [name for name in names if any(fnmatch(name, pattern) for pattern in patterns)]\n\nprint(matches)\n<\/code><\/pre>\n<\/div>\n<p>Use the same idea for exclusions by negating a match. For example, keep names where <code>not fnmatch(name, \"*.tmp\")<\/code> is true. This keeps include and exclude rules explicit.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"fnmatch_vs_glob\"><\/span>fnmatch vs glob<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>fnmatch<\/code> checks a string against a pattern. <code>glob<\/code> searches the filesystem and returns matching paths. Use <code>glob<\/code> when the pattern itself should find files, and use <code>fnmatch<\/code> when you already have names and want a lightweight matcher. The <a href=\"https:\/\/docs.python.org\/3\/library\/glob.html\">glob documentation<\/a> explains recursive path expansion and filesystem search behavior.<\/p>\n<p>For most file-management scripts, a practical split is simple: collect names with <code>pathlib<\/code>, <code>os.listdir()<\/code>, or <code>glob<\/code>, then filter with <code>fnmatch<\/code> if the selection logic is easiest to express with shell-style wildcards. Keep in mind that <code>fnmatch<\/code> does not validate user input for security by itself. It only answers whether a string matches a pattern.<\/p>\n<p>The <code>fnmatch<\/code> module is best for readable wildcard checks such as <code>*.py<\/code>, <code>data_??.csv<\/code>, and <code>test_*<\/code>. Reach for <code>fnmatchcase()<\/code> when case must be exact, <code>filter()<\/code> for one-pattern list filtering, and <code>translate()<\/code> when a wildcard pattern needs to become a compiled regular expression.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/fnmatch-filter-b192.png\" alt=\"Python Pool infographic mapping a directory listing through fnmatch filter to selected files\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Filter directory names with a pattern when only matching filenames are needed.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Match_Common_Filename_Patterns\"><\/span>Match Common Filename Patterns<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>fnmatch() answers whether one name matches a pattern. Patterns describe the whole string, so include the suffix or prefix you intend to accept. Keep patterns readable and treat them as data when they come from configuration.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from fnmatch import fnmatch\n\nnames = [\"app.py\", \"README.md\", \"test_api.py\", \"data.csv\"]\nfor name in names:\n    if fnmatch(name, \"*.py\"):\n        print(name)<\/code><\/pre>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Use_Wildcards_And_Character_Classes\"><\/span>Use Wildcards And Character Classes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>An asterisk matches any sequence, a question mark matches one character, and bracket expressions match a selected character set or range. Test representative names, including empty or unusually long names, before using a pattern to include or delete files.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from fnmatch import fnmatch\n\npatterns = [\"report-??.csv\", \"image[0-9].png\", \"[!_]*\"]\nvalues = [\"report-01.csv\", \"image7.png\", \"_private.txt\"]\nfor pattern in patterns:\n    print(pattern, [value for value in values if fnmatch(value, pattern)])<\/code><\/pre>\n<\/div>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/fnmatch-check-b192.png\" alt=\"Python Pool infographic testing case sensitivity, paths, hidden files, and validation\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Check case behavior, path components, hidden files, and the intended pattern scope.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Filter_Lists_And_Control_Case\"><\/span>Filter Lists And Control Case<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>fnmatch.filter() is convenient for a list of names. fnmatch() applies operating-system case normalization, while fnmatchcase() always compares case exactly. Decide whether a pattern such as *.PY should match file.py, and test on the systems where the result matters.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import fnmatch\n\nnames = [\"README\", \"readme\", \"Readme\"]\nprint(fnmatch.filter(names, \"readme\"))\nprint([name for name in names if fnmatch.fnmatchcase(name, \"readme\")])<\/code><\/pre>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Keep_Path_Boundaries_Explicit\"><\/span>Keep Path Boundaries Explicit<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>fnmatch treats its input as a string and does not turn a wildcard into a safe recursive file operation. Match Path.name when only the filename should matter, or use a path-aware traversal and apply a clear root, extension, and symlink policy before mutating files.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from pathlib import Path\nfrom fnmatch import fnmatch\n\nroot = Path(\"reports\")\nfor path in root.rglob(\"*\"):\n    if path.is_file() and fnmatch(path.name, \"*.csv\"):\n        print(path)<\/code><\/pre>\n<\/div>\n<p>Python&#8217;s <a href=\"https:\/\/docs.python.org\/3\/library\/fnmatch.html\">fnmatch documentation<\/a> defines shell-style patterns, case handling, filter(), and translate(). For recursive discovery, compare the path-aware behavior of <a href=\"https:\/\/docs.python.org\/3\/library\/glob.html\">glob<\/a> before using a string match as a filesystem operation.<\/p>\n<p>For related filename workflows, compare <a href=\"https:\/\/www.pythonpool.com\/python-listdir\/\">os.listdir()<\/a>, <a href=\"https:\/\/www.pythonpool.com\/python-check-if-file-exists\/\">file-existence checks<\/a>, and <a href=\"https:\/\/www.pythonpool.com\/python-touch-file\/\">path creation with pathlib<\/a> when a wildcard match becomes a filesystem operation.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"What_is_Python_fnmatch_used_for\"><\/span>What is Python fnmatch used for?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>It matches strings such as filenames against shell-style wildcard patterns including *, ?, and character classes in square brackets.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_difference_between_fnmatch_and_fnmatchcase\"><\/span>What is the difference between fnmatch() and fnmatchcase()?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>fnmatch follows operating-system case-normalization rules, while fnmatchcase performs a case-sensitive match without normalizing case.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_filter_filenames_with_fnmatch\"><\/span>How do I filter filenames with fnmatch?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Use fnmatch.filter(names, pattern) for a list of names, or iterate and call fnmatch() when each result needs additional handling.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Can_fnmatch_match_full_paths\"><\/span>Can fnmatch match full paths?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>It matches strings, but path separators are not special to fnmatch; use pathlib, glob, or a path-aware policy when directory boundaries matter.<\/p>\n<p><script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"What is Python fnmatch used for?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"It matches strings such as filenames against shell-style wildcard patterns including *, ?, and character classes in square brackets.\"}},{\"@type\":\"Question\",\"name\":\"What is the difference between fnmatch() and fnmatchcase()?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"fnmatch follows operating-system case-normalization rules, while fnmatchcase performs a case-sensitive match without normalizing case.\"}},{\"@type\":\"Question\",\"name\":\"How do I filter filenames with fnmatch?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Use fnmatch.filter(names, pattern) for a list of names, or iterate and call fnmatch() when each result needs additional handling.\"}},{\"@type\":\"Question\",\"name\":\"Can fnmatch match full paths?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"It matches strings, but path separators are not special to fnmatch; use pathlib, glob, or a path-aware policy when directory boundaries matter.\"}}]}<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use Python&#8217;s fnmatch library to match filenames with shell-style wildcards, filter lists, handle case rules, and avoid path mistakes.<\/p>\n","protected":false},"author":38,"featured_media":33464,"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 v28.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python fnmatch: Match Filenames with Wildcards<\/title>\n<meta name=\"description\" content=\"Use Python&#039;s fnmatch library to match filenames with shell-style wildcards, filter lists, handle case rules, and avoid path mistakes.\" \/>\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=\"Python fnmatch: Match Filenames with Wildcards\" \/>\n<meta property=\"og:description\" content=\"Use Python&#039;s fnmatch library to match filenames with shell-style wildcards, filter lists, handle case rules, and avoid path mistakes.\" \/>\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=\"2026-07-13T07:13:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-fnmatch-library.png\" \/>\n<meta name=\"author\" content=\"Namrata Gulati\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Python Pool\" \/>\n<meta name=\"twitter:description\" content=\"Practical Python tutorials, error fixes, code examples, and project guides.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-fnmatch-library.png\" \/>\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\":\"Python fnmatch: Match Filenames with Wildcards\",\"datePublished\":\"2023-09-24T08:53:39+00:00\",\"dateModified\":\"2026-07-13T07:13:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-fnmatch-library\\\/\"},\"wordCount\":1130,\"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\\\/2026\\\/07\\\/python-fnmatch-library-guide-pythonpool.png\",\"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\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-fnmatch-library\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/python-fnmatch-library\\\/\",\"name\":\"Python fnmatch: Match Filenames with Wildcards\",\"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\\\/2026\\\/07\\\/python-fnmatch-library-guide-pythonpool.png\",\"datePublished\":\"2023-09-24T08:53:39+00:00\",\"dateModified\":\"2026-07-13T07:13:21+00:00\",\"description\":\"Use Python's fnmatch library to match filenames with shell-style wildcards, filter lists, handle case rules, and avoid path mistakes.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-fnmatch-library\\\/#breadcrumb\"},\"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\\\/2026\\\/07\\\/python-fnmatch-library-guide-pythonpool.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-fnmatch-library-guide-pythonpool.png\",\"width\":1350,\"height\":650,\"caption\":\"Python fnmatch library guide showing wildcard patterns and matched file names\"},{\"@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\":\"Python fnmatch: Match Filenames with Wildcards\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"name\":\"Python Pool\",\"description\":\"Practical Python tutorials, error fixes, code examples, and project guides.\",\"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\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"contentUrl\":\"https:\\\/\\\/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:\\\/\\\/secure.gravatar.com\\\/avatar\\\/19c5e6bfbc6202d4017b79f726b2ad5e520491d67ff428a87c071afef23ecd89?s=96&d=wavatar&r=g\",\"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\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python fnmatch: Match Filenames with Wildcards","description":"Use Python's fnmatch library to match filenames with shell-style wildcards, filter lists, handle case rules, and avoid path mistakes.","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":"Python fnmatch: Match Filenames with Wildcards","og_description":"Use Python's fnmatch library to match filenames with shell-style wildcards, filter lists, handle case rules, and avoid path mistakes.","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":"2026-07-13T07:13:21+00:00","og_image":[{"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-fnmatch-library.png","type":"","width":"","height":""}],"author":"Namrata Gulati","twitter_card":"summary_large_image","twitter_title":"Python Pool","twitter_description":"Practical Python tutorials, error fixes, code examples, and project guides.","twitter_image":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-fnmatch-library.png","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":"Python fnmatch: Match Filenames with Wildcards","datePublished":"2023-09-24T08:53:39+00:00","dateModified":"2026-07-13T07:13:21+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/"},"wordCount":1130,"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\/2026\/07\/python-fnmatch-library-guide-pythonpool.png","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","@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/","url":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/","name":"Python fnmatch: Match Filenames with Wildcards","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\/2026\/07\/python-fnmatch-library-guide-pythonpool.png","datePublished":"2023-09-24T08:53:39+00:00","dateModified":"2026-07-13T07:13:21+00:00","description":"Use Python's fnmatch library to match filenames with shell-style wildcards, filter lists, handle case rules, and avoid path mistakes.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-fnmatch-library\/#breadcrumb"},"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\/2026\/07\/python-fnmatch-library-guide-pythonpool.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-fnmatch-library-guide-pythonpool.png","width":1350,"height":650,"caption":"Python fnmatch library guide showing wildcard patterns and matched file names"},{"@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":"Python fnmatch: Match Filenames with Wildcards"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Practical Python tutorials, error fixes, code examples, and project guides.","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":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"https:\/\/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:\/\/secure.gravatar.com\/avatar\/19c5e6bfbc6202d4017b79f726b2ad5e520491d67ff428a87c071afef23ecd89?s=96&d=wavatar&r=g","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"}}]}},"_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":68,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/27973\/revisions"}],"predecessor-version":[{"id":42001,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/27973\/revisions\/42001"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/33464"}],"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}]}}