how to use python fnmatch module

Posted by Suraj Singh on May 21, 2017 · 13 mins read
hii readers,




                           In This Tutorial, I am going to tell you about, what is python fnmatch module? and how to use python fnmatch module?

Python Fnmatch Module Tutorial


Q 1. What is fnmatch module?


Ans. Python fnmatch module is a python standard library comes with python. This Module Allow user to compare file & Folder names With Unix Shell-Style Pattern. With This Module, A User Can Easily Compare File and Folder Names With Specific Pattern.

So, let's Start Our Practical Tutorial.




Input : [1]  


#
# ==================================================
# PYTHON fnmatch MODULE TUTORIAL
# ==================================================
#
# author : suraj singh bisht
# contact :
# surajsinghbisht054@gmail.com
# https://www.bitforestinfo.com
#
# Here, For This Tutorial
#
# I am using
#
# Operating System : Ubuntu 16.04
# Python Version : python 2.7.12
# Editor : ipython notebook
#

Output : [1]  

  

Input : [2]  

#
# Import Module
#
import fnmatch
import os

Output : [2]  

  

Input : [3]  

#
# fnmatch Module allow user to compare file and folders
# names with given specific pattern.
# fnmatch module supports Unix shell-style Patterns For
# Comparing File And Folders Names.
#
# In Short, This Module Allow You to Search File and Folders
# Name is Specific patterns.
#
# So, Let Me show you A Example.
# But First, We needs Some Files To Test Glob Module
#
#
for i in ['file1.txt','file2.doc','file3.py','file4.extension','123456.c','Test123.txt']:
f = open(i,'w')
f.close()
# Now, we Are Ready To Test Glob Module.
# So, let's Start
# In This Example, I will try To find any text file that's
# Contains .txt Extension.
#

Output : [3]  

  

Input : [4]  

#######################
# Refrence Of Pattern #
#######################
#---------------------------------------
# Pattern| Meaning |
#---------------------------------------
# * | Match Every Character |
#---------------------------------------
# ? | Match Any Single Character |
#---------------------------------------
# [seq] | Any Character From Sequence |
#---------------------------------------
# [!seq] |Any Character Not In Sequence |
#---------------------------------------
#

Output : [4]  

  

Input : [5]  

#
# Here,First I Am Showing Simple usages Of Os.listdir function.
# os.listdir() function provide us to list of file and folders present
# In any specific path given as argument.
#
print "printing All Files And Folders, Present In Current Directory : "
#
#
for i in os.listdir("."):
print " ==> ",i

Output : [5]  

printing All Files And Folders, Present In Current Directory : 
==> file3.py
==> .ipynb_checkpoints
==> file4.extension
==> 123456.c
==> fnmatch_part_1.ipynb
==> Test123.txt
==> file1.txt
==> file2.doc

Input : [6]  

#
#
# Now, let's Try to find file that contain '.txt' extension
#

for i in os.listdir("."):
if fnmatch.fnmatch(i, '*.txt'):
print " Txt File ==> ",i

Output : [6]  

 Txt File ==>   Test123.txt
Txt File ==> file1.txt

Input : [7]  

#
# As You can See In Our previous Example, we Successfully find
# txt file, present in our current directory.
#
# Now,Let's try More Situations:
#
for i in os.listdir("."):
if fnmatch.fnmatch(i, '*.txt'):
print "\n Txt File ==> ",i


elif fnmatch.fnmatch(i, '*.c'):
print "\n C Codes ==> ",i


elif fnmatch.fnmatch(i, '*.py'):
print "\n Python Script ==> ",i


elif fnmatch.fnmatch(i, '*.doc'):
print "\n Doc File ==> ",i

else:
print "\n Unknown File ==> ",i


Output : [7]  

 Python Script ==>   file3.py

Unknown File ==> .ipynb_checkpoints

Unknown File ==> file4.extension

C Codes ==> 123456.c

Unknown File ==> fnmatch_part_1.ipynb

Txt File ==> Test123.txt

Txt File ==> file1.txt

Doc File ==> file2.doc

Input : [8]  

# This Tutorial Ends Here,
#
# For Reference:
#
# Python Official Documentation
#
# https://docs.python.org/3/library/fnmatch.html#module-fnmatch
#
#

Output : [8]  

  



Written By