Python 3 Script to Delete All Empty Folders in a Directory Using OS Module on Terminal
16 views
Jun 1, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you a Python
0:04
script which will automatically delete
0:06
empty folders from any sort of directory
0:09
or any sort of path so empty folders can
0:12
be a very tedious task because they can
0:15
be just be consuming space without um it
0:20
will be very frustrating process so this
0:22
Python script will automatically scan
0:24
your directory and delete those empty
0:26
folders which are there let me walk you
0:29
through the Python script for this we
0:31
are using the operating system module
0:34
and then we specify the folder name
0:37
which you are targeting you just need to
0:39
give the name of the folder or the full
0:42
path after that we simply get the total
0:46
number of folders which are there in
0:48
this directory so using operating system
0:51
walk method we use this and pass the
0:54
root location and then we simply say one
0:58
colon and this will give you a total
1:01
number of folders which are there inside
1:03
this after that we loop through each
1:07
folder and like this and basically we
1:10
print the content of each folder and if
1:13
the folder is empty then we delete simp
1:16
delete that folder very simple very
1:18
simple
1:19
logic and
1:21
uh this is the condition right here a
1:25
folder
1:26
two so if the folder is empty we simply
1:29
call the remove directory function of
1:32
Python in the operating system module we
1:35
give the folder name that's
1:38
all so now if you run this uh if you put
1:42
dot here this will treat the current
1:45
working directory the root directory it
1:47
will scan for all the empty folders
1:49
which are there
1:52
so as you can see in this there is one
1:56
empty folder right here which is this
1:58
folder it doesn't contain anything if I
2:00
try to execute this script
2:07
here first of all I have given the
2:10
script in the description of the
2:17
video so let me just run this app
2:25
27 so you will see it will
2:27
delete all these empty
2:37
folders so this is actually the script
2:40
uh you can do this very easily inside
2:43
Python and also check out my website
2:49
uh freemediatools.com
2:52
uh which contains thousands of tools
#Computers & Electronics