Python 3 Script to Delete All Files Older than N Days in a Directory on Terminal
32 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 delete all the files
0:07
which are older than X number of days So
0:11
it can be either older than two 2 days 3
0:14
days 4 days or older than a week older
0:17
than 10 days 10 days You can replace
0:21
this value right here So in the n value
0:24
so it can be any value Let's suppose 3
0:26
days 4 days 5 days 6 days So it will
0:30
only delete those files which are older
0:32
than this X amount of days So it's a
0:35
very important script It will save you a
0:37
lot of time and it will run inside the
0:40
background and it will just search for
0:42
those files which are no longer needed
0:44
by you and it's older than three or 1
0:47
week older than a month Those files will
0:51
automatically be deleted by this Python
0:53
script So I've given this script in the
0:54
description of the video Now let me show
0:57
you how I built
0:59
this For this you only need the built-in
1:02
modules which are operating system and
1:04
also the time module So just import both
1:07
these
1:09
packages Uh after that here you need to
1:11
mention which folder you are working
1:13
with You need to give the path of that
1:15
folder So I'm just give the name of the
1:19
folder or you can even paste the
1:21
complete path as well After that you
1:23
specify the value fn right here This is
1:27
the number of
1:31
days older than this amount of days The
1:35
script will automatically delete only
1:37
those files which are older than 3 days
1:40
And then now we will simply go to this
1:42
directory
1:45
chdr and we will go to that particular
1:48
folder First of all by this code here we
1:51
say get current working
1:53
directory and move to that particular
1:57
folder After moving to that folder we
1:59
simply get list of those files which we
2:02
want to
2:05
delete We get all the files here Now we
2:08
will compare the time here that you
2:12
mentioned And for this we'll use the
2:14
time module time dot
2:17
time we'll get the current time and then
2:21
in each day as you all know it's 8 8 6 4
2:26
0 seconds are
2:28
there So here we will looping through
2:30
all the files here in this for
2:32
loop and then we'll be comparing the
2:35
time here If the time comes out to be
2:37
larger than 3 days then that file will
2:40
automatically get deleted So here we are
2:42
simply getting that get current working
2:46
directory We getting access to that file
2:49
and now we are simply comparing the time
2:52
So for this we use the stat method and
2:55
here we simply pass the
2:57
location and then we calculate the time
3:00
here using this function
3:02
st m time That's all After that we
3:08
simply now we got this time here of the
3:11
file which when the file was created Now
3:14
we can simply inside this if condition
3:16
we can compare that if that
3:18
time is uh smaller than the current time
3:22
that we mentioned minus the day
3:27
multiplied by n So this is simple logic
3:30
mathematical logic comparing the time
3:33
period when the time when the file was
3:37
created If the value evaluates to true
3:40
in that case the file will be deleted
3:44
and then we will use this remove
3:46
function to delete the
3:48
file That's all So this is the complete
3:51
script here
3:53
Uh very simple We are using simple logic
3:56
to do this And now if you want to
3:59
customize the total number of days you
4:01
just need to change at the sing single
4:05
location So if you want to delete only
4:08
files older than 7 days you will put
4:10
seven here to 30 days 30 here So this
4:14
script will save you a lot of time It
4:16
will just run this in the background You
4:19
you no longer manually need to delete
4:21
the older files the script will
4:22
automatically do for you So I've given
4:25
all the source code in the description
4:26
of the video Uh and also check out my
4:31
website freemediattools.com
4:34
uh which contains thousands of tools