Automate Removal of Old Files with Python
To keep directories organized you can make a Python program that searches for old files in a directory and removes them. You can choose to delete them or move them over to an archive directory.

An example of a directory where we don’t want old files
Suppose we have a directory that is used to store images. We only want to have recent images and so we need a program that removes files if they are older than, let’s say 300 hours.
The 5 systems of our directory cleaning program
Our program that removes old files from a directory will consist of 5 systems:
- a system that takes a directory’s path and an age-threshold as input
- a system that lists all files in the directory
- a system that checks when a file was last modified
- a system that checks whether a file is older than the threshold
- a system to remove a file from the directory, either by moving it or deleting it
1. A system that takes a directory’s path and an age-threshold as input
Our program will be started from command line like: py cleanup.py
We will allow input data to be passed along with that command:
- The path of the directory that needs to be cleaned. For example:
C:/Users/BE/Documents/Images - The age-threshold as a number of hours. If we take an age-threshold of 300 hours, files that are last modified longer than 300 hours ago must be removed.
An example command to start our script would be:
py cleanup.py C:/Users/BE/Documents/Images 300
We can collect the input arguments in our script with the sys package’s argv list.
import sys
print(sys.argv)
directory_path = sys.argv[1]
age_threshold = int(sys.argv[2])
print(directory_path)
print(age_threshold)This prints:
['files.py', 'C:/Users/BE/Documents/Images', '300'] C:/Users/BE/Documents/Images 300
Which means we have succesfully collected the input data in variables directory_path and age_threshold. Notice that we transformed the age_threshold into an integer, this makes it possible to use in calculations later on.
To learn more about this system you can read this article:
2. A system that lists all files in a directory
We can easily make a list of the contents of a directory by using the listdir function from the os package. All we have to do is specify the path of the directory to the listdir function and it will return a list of filenames.
import os
files = os.listdir(directory_path)
print(files)In the above code the list files contains the names of the files in the Images directory:
['apple.jpg', 'bee.jpg', 'blueprint.jpg', 'bunny.jpg', 'business man.jpg',
'cards.jpg', 'chatting robot.png', 'chef.jpg', 'construction worker.jpg',
'dessert.jpg', 'dog.jpg', 'finance graph.jpg', 'friends_teamwork.jpg',
'furniture.jpg', 'house.jpg', 'laptop.jpg', 'megaphone.jpg', 'open book.jpg',
'people taking a picture.jpg', 'pizza.jpg', 'python.jpg', 'python.png',
'rooster.jpg', 'snake.jpg', 'tablet.jpg', 'working on computer.jpg',
'working on laptop.png', 'working_on_a_computer.jpg']3. A system that checks when a file was last modified
To determine when a file was last modified we can use the stats generated by the stat function from the os package. We have to specify a file’s path to that function for it to return the stats.
As an example we will check the stats of the file apple.jpg:
directory_path = 'C:/Users/BE/Documents/Images'
filename = 'apple.jpg'
filepath = directory_path + '/' + filename
stats = os.stat(filepath)
print(stats)This is what it prints:
os.stat_result(st_mode=33206, st_ino=6755399441183522, st_dev=4169482404,
st_nlink=1, st_uid=0, st_gid=0, st_size=45283, st_atime=1676276165,
st_mtime=1675202684, st_ctime=1676214663)The last modification datetime is stored in st_mtime. It is formatted as number of seconds since January 1st, 1970, but that is fine as you will see later.
Here is how to extract st_mtime:
stats = os.stat(filepath)
last_modified = stats.st_mtime
print(last_modified)This correctly prints: 1675202684.7658122
4. A system that checks whether a file is older than the threshold
In the system above we have the last modification datetime of a file in a number of seconds since January 1st, 1970.
Now we will take the current number of seconds since January 1st, 1970. This can easily be done with the time function from the time package:
import time
current_time = time.time()
print(current_time)The above code prints: 1676287271.174691.
We can compare current_time with the last_modified time to see how old a file is.
file_age_in_seconds = current_time - last_modified
print(file_age_in_seconds)The above code prints: 1084801.2706189156. If we divide that by 3600 (the number of seconds in an hour) we get the age in number of hours.
file_age_in_hours = file_age_in_seconds / 3600
print(file_age_in_hours)The above code prints: 301.3610313791037.
We can check whether a file should be removed with a simple if statement.
if file_age_in_hours > age_threshold:
#FILE REMOVAL TO BE DONE HERECombining the first 4 systems of our program
As a test, we can now combine the above 4 systems to print the age of each file and whether it should be removed.
import sys
import os
import time
directory_path = sys.argv[1]
age_threshold = int(sys.argv[2])
files = os.listdir(directory_path)
for filename in files:
filepath = directory_path + '/' + filename
stats = os.stat(filepath)
last_modified = stats.st_mtime
current_time = time.time()
file_age_in_seconds = current_time - last_modified
file_age_in_hours = file_age_in_seconds / 3600
if file_age_in_hours > age_threshold:
#FILE REMOVAL TO BE DONE HERE
print(filename + ' should be removed, age: ' + str(file_age_in_hours))
else:
print(filename + ' should not be removed, age: ' + str(file_age_in_hours))If we now run the command:
py cleanup.py C:/Users/BE/Documents/Images 300
We can check the results and see that everything works correctly:
apple.jpg should be removed, age: 301.6782086233298 bee.jpg should be removed, age: 333.92660351269774 blueprint.jpg should be removed, age: 303.10386330160827 bunny.jpg should not be removed, age: 284.10428212549954 business man.jpg should be removed, age: 336.23683873163327 cards.jpg should be removed, age: 324.87590992814967 chatting robot.png should not be removed, age: 150.8007668226295 chef.jpg should not be removed, age: 289.6990299455987 construction worker.jpg should not be removed, age: 290.39340113580226 dessert.jpg should not be removed, age: 284.4691707797845 dog.jpg should be removed, age: 333.0312643531958 finance graph.jpg should be removed, age: 352.5994907921553 friends_teamwork.jpg should be removed, age: 310.5233200907045 furniture.jpg should not be removed, age: 288.515792476005 house.jpg should not be removed, age: 288.7089229124122 laptop.jpg should not be removed, age: 194.71422586116526 megaphone.jpg should not be removed, age: 287.165513715148 open book.jpg should not be removed, age: 290.7236293404632 people taking a picture.jpg should be removed, age: 311.18775296535756 pizza.jpg should be removed, age: 303.32271976265645 python.jpg should not be removed, age: 265.8646370517545 python.png should not be removed, age: 265.8306313452456 rooster.jpg should not be removed, age: 284.54089893281457 snake.jpg should not be removed, age: 265.8681665019857 tablet.jpg should not be removed, age: 285.6957534173462 working on computer.jpg should be removed, age: 314.8451126029094 working on laptop.png should not be removed, age: 168.1809028936757 working_on_a_computer.jpg should be removed, age: 374.45141981489127
All files with an age higher than 300 hours are mentioned to be removed.
5. A system to remove a file from the directory
Now we will replace the print statements with the real deal. Let’s add the system that removes the old files.
I choose to remove the files from the directory by deleting them. You could also move it to another directory, check out this article if you want to learn how:
To delete a file, simply call the remove function from the os package and specify the file’s path: os.remove(filepath)
Testing our old file removal program
If we implement the final system, our final version of the program looks like this:
import sys
import os
import time
directory_path = sys.argv[1]
age_threshold = int(sys.argv[2])
files = os.listdir(directory_path)
for filename in files:
filepath = directory_path + '/' + filename
stats = os.stat(filepath)
last_modified = stats.st_mtime
current_time = time.time()
file_age_in_seconds = current_time - last_modified
file_age_in_hours = file_age_in_seconds / 3600
if file_age_in_hours > age_threshold:
os.remove(filepath)Let’s test it by running this command:
py cleanup.py C:/Users/BE/Documents/Images 300
And… it worked. The 12 files that were older than 300 hours are gone. And the other 16 files are still there.
We can now run this script whenever we want to clean a directory. We can also use it for different directories or age thresholds by simply changing the input arguments.
The program can also be scheduled or you can let it run inside of a never ending loop that repeats the code at certain intervals like we did in:
Thank you for reading!
You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:





