Python 3 Glob Module Script to List All Files of a Specific Extension in a Folder Using Glob Pattern
345 views
Jun 3, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
so in this video I will show you how to
0:04
get the list of a specific amount of
0:07
files belonging to a specific
0:11
extension using the globe module inside
0:14
Python so it will list out all the files
0:16
of a specific extension in a folder
0:18
using a glob
0:20
pattern so this is actually a very
0:23
simple script here that we are only
0:25
requesting a specific extension let me
0:28
replace here dot PDF so it will search
0:31
the entire directory right here and it
0:34
will only return the files which are
0:36
ending with PDF so if I run the script
0:45
here so right here if I run this let me
0:49
just delete this
0:54
so just write here python.app
0:59
py so sorry the extens script name is
1:05
python app dot 20 py so it will fetch
1:10
all the files which are ending with pdf
1:13
here if you see it has searched all this
1:15
directory and it has splitted all the
1:18
pdf files this is the one pdf file this
1:21
is the
1:23
second so here it basically find out
1:26
only the PDF files so here you can
1:29
replace it let's suppose I only want the
1:32
CSV file so this glob pattern this glob
1:35
module will only request this CSV file
1:38
so it only generated basically find out
1:42
this single file
1:44
so this is the glob module which is very
1:47
useful in finding or filtering out files
1:50
belonging to a specific extension
1:53
so it I think it comes within the Python
1:56
default installation so you can import
1:58
this by using this module
2:01
glob or I
2:03
think you need to install it as well
2:07
just search
2:08
it yeah so it comes within the default
2:11
installation and then you specify
2:14
whatever
2:16
path the extension of files you want to
2:19
this is a very basic pattern here we are
2:22
only requesting files ending with PDF so
2:27
there are also more advanced patterns as
2:31
well of
2:33
globe so here we simply print
2:37
out like this
2:40
or a single line we print out all the
2:43
files we use this
2:50
function and then we loop through each
2:52
file you belonging to this glob pattern
2:55
by using this
2:57
function we get the
2:59
path make the recursive to be
3:03
true and we simply print out the file so
3:07
this is the overall
3:09
script you will see that
3:15
so so this property here recursive to
3:18
true it it tells it to actually find
3:22
out let's suppose if you have a
3:24
directory right
3:29
here inside this
3:33
directory there is another PDF file
3:35
which is present
3:38
so if I copy this
3:41
file copy this PDF file and paste it
3:44
right here but then also this recursive
3:47
property to true will make sure that it
3:49
will search this PDF file as well so you
3:53
would see that it will also search this
3:56
file as well
4:01
so this is actually the blog module
4:04
where you can actually find out files
4:07
belonging to a specific extension so
4:09
thank you very much for watching this
4:11
video and also check out my website uh
4:16
freemediatools.com uh which contains
4:18
thousands of tools
#Computers & Electronics
#Programming
#Scripting Languages