how to use python glob module?

Namaste Friends,



In This Post, I am going to tell you about what is python glob module? and How to use python glob module?

Python Glob Module Tutorial


Q 1. What Is Python Glob Module?


Ans. Python Glob Module is a Python Standard library, Come With Python. This Module Allow User To Find Any File & Folder From Local Disk Using Special Unix Shell Style Pattern. With This Types of Pattern, A User Can Find any File And Folder In Very Accurate Way. This Module is also Based On another Python module called fnmatch Module. In Simple Words, This Module Provides Special Functions To Find Files And Folders Using Unix Shell Supported Pattern In All Types of Operating System.


So, let's start Our Practical Examples Tutorials.



Input : [1]  


#
# ==================================================
#          PYTHON Glob MODULE TUTORIAL
# ==================================================
# 
# author   : suraj singh bisht
# contact  : SSB
#            surajsinghbisht054@gmail.com
#            https://bitforestinfo.blogspot.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 glob

Output : [2]  

  

Input : [3]  

#
# Glob Module allow user to search files and folders, 
# Using Unix Or Regular Experssion Like Patterns,
# In The Place Of File Name.
# Means With The Help Of Glob, A user Can Search Files
# That Can match With Specific Pattern.
# 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.
#
print glob.glob("*.txt")

Output : [3]  

['Test123.txt', 'file1.txt']

Input : [4]  

# So, As You Can See In previous Example, WE Got 2 File Name 
# As The Result. In Previous Example We used "*.txt" Pattern
# Where "*" Denote any character, and ".txt" denotes file
# Extension.
# let's Try Another Example
#
# In This Example, I am Going To Show You Different Types Of 
# Situtation In One Example

print "[1] File Contain '.py' Extension = ", glob.glob("*.py")
print "[2] File Contain '.c' Extension = ", glob.glob("*.c")
print "[3] File Contain '.doc' Extension = ", glob.glob("*.doc")
print "[4] File Contain Any Extension = ", glob.glob("*.*")
print "[5] File Using 6 Numbers As Filename Extension = ", glob.glob("[1-9][1-9][1-9][1-9][1-9][1-9].c")

Output : [4]  

[1] File Contain '.py' Extension =  ['file3.py']
[2] File Contain '.c' Extension =  ['123456.c']
[3] File Contain '.doc' Extension =  ['file2.doc']
[4] File Contain Any Extension =  ['file3.py', 'file4.extension', '123456.c', 'glob_part_1.ipynb', 'Test123.txt', 'file1.txt', 'file2.doc']
[5] File Using 6 Numbers As Filename Extension =  ['123456.c']

Input : [5]  

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

Output : [5]  

  

Input : [6]  

#
# As You Can See In Above Example, With This Module We 
# Can do Search Files And Folders In Any Operating   
# System, according to Unix Shell rules. 
#
# For More Details:
#
# Visit Here : 
#         https://docs.python.org/3/library/glob.html
#
#

Output : [6]  

  


Written By
                  SSB

Share this

Related Posts

Previous
Next Post »