Remove old files using Python

Posted by on

One of the biggest headache computer users have is that their disk contains a lot of old and unused files. We download a lot of files from internet be it zip files or pdf files. And we are quite lazy to search old files and delete them.

Let’s see how we can remove old files using Python.

This python script intends to speed up this quite boring task. We will accept 3 inputs from the user.

  1. Directory from which files are to be removed. One of the limitation of this script is it does not iterate directories. However, this is not a big deal and script can be modified a bit to accommodate this requirement.
  2. Extension of files to be removed. This script accepts only one extension. Again small adjustments need to be made so that you can process multiple extensions.
  3. Get staleness of file i.e. how old files you want to remove

Experiment Setup:

  1. Python 3
  2. IDE: Spyder
  3. Libraries Used:
    1. os
    2. time
    3. datetime
    4. dateutil

Limitation: This script is tested only on Windows. I have not tested it on Mac or Linux. But again it should not be very difficult to do so.

Full Script to remove old files using Python:

import os
import time
import datetime
import dateutil.relativedelta

# Get path from user
# Sample input: C:\\Users\\ABC\\Downloads\\
path = input('Enter the path of directory (Type \\ for \): ')

# Get extension of files that you want to remove
# Sample input: zip
extension = input('Enter the extension of the files you want to remove: ')

# Get staleness of file i.e. how old files you want to remove
# Sample input (months): 2.5
staleness = float(input('Remove files older than (months): '))

# Find files in the directory with the given extension
files = [file for file in os.listdir(path) if file.endswith(extension)]

# Iterate through files and find how old they are using relative delta 
# in dateutil library of python
for file in files:
    # Get creation date of file
   creation_date = datetime.datetime.fromtimestamp(os.path.getatime(path+file))
   # Get todays date and time
   today = datetime.datetime.fromtimestamp(time.time())
   # Find the difference between above two datetimes using relativedelta
   time_past_since_creation = dateutil.relativedelta.relativedelta(today, creation_date)
   # Relative delta returns months, days, hours, minutes seconds, microseconds
   # We will convert days into months by dividing days by 30
   # 30 is an approximation
   months_since_creation = time_past_since_creation.months + time_past_since_creation.days/3
   
   # If months since creation of the file exceeds the staleness input bu user
   # delete the file
   if months_since_creation > staleness:
        os.remove(path+file)

Note:

I am no more writing regarding Python or programming on thisĀ blog, as I have shifted my focus from programming to WordPress and web development. If you are interested in WordPress, you can continue reading other articles on this blog.
Thanks and Cheers ????

>