Python 3 OS Module Script to Delete All Files in a Folder With Specific Extension in Terminal
128 views
Jun 1, 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 a
0:03
Python script which will automatically
0:05
rename or
0:08
delete all the files which are ending
0:10
with a certain extension so let's
0:12
suppose if you want to delete all the
0:14
files which are images let's suppose
0:17
which ends from PNG it's a very simple
0:20
script here let me show you in Python so
0:22
simply create a pi file here and for
0:25
this you don't require any third party
0:27
module you simply require the operating
0:29
system module you simply require it at
0:31
the very top after that you specify the
0:34
folder path which folder you are working
0:38
with so now let me just
0:41
make put the folder name here so I am
0:44
just working inside
0:47
uh let's
0:49
suppose I am working inside this
0:53
folder and inside this files folder I
0:57
create some a bunch of txt
1:00
files so I create these two files here
1:03
which are ending with txt if you see
1:05
this is the first file this is the
1:07
second file i put this some text here
1:11
so now I'm targeting this folder here
1:13
which is files folder and now need to
1:16
target these txt files and only delete
1:18
these files so what I will say is for
1:21
file name and operating system we using
1:23
this method list directory it will list
1:26
all the files inside this folder folder
1:29
path and here we'll put this if
1:31
condition that only target those files
1:35
which ends with so this contains this
1:38
function here ends with and here you can
1:40
target any extension so in this example
1:44
we are only targeting the files which
1:45
are ending with txt so text files so
1:50
here after that we simply call the
1:53
remove function so this will delete this
1:55
file and uh we will join the full path
1:59
here which is folder path plus the file
2:03
name that's all and after that we can
2:06
print out a simple statement that your
2:08
file has been
2:10
deleted uh that's a very simple Python
2:13
script it will scan your directory your
2:16
folder and then it will only target
2:18
those files which are ending with this
2:20
extension that you put right here in the
2:22
code so if I try to run this Python code
2:26
here so now just notice in the files
2:29
directory these two files only these two
2:32
files will be deleted
2:35
so I delete the sorry call
2:38
this so now that two files are
2:41
successfully deleted guys rest of the
2:43
files remain constant so this is very
2:46
simple Python script by which you can
2:48
target specific files to delete in a
2:50
specific folder so this is actually your
2:53
Python script uh thank you very much for
2:56
watching this video please hit that like
2:58
button subscribe the channel and also
3:00
check out my website
3:03
freemediattools.com uh which contains
3:06
thousands of tools
#Software