We earn commission when you buy through affiliate links.

This does not influence our reviews or recommendations.Learn more.

Cleaning file system regularly manually is not good.

Deleting files and folders manually is not an exciting task, as one may think.

It makes sense to automate them.

Here comes Python to make our lives easier.Pythonis an excellent programming language for scripting.

We are going to take advantage of Python to finish our task without any obstacle.

First, you should know whyPythonis a good choice.

We can replace any annoying or repetitive system tasks using Python.

Writing scripts for completing a specific system task is a cupcake if you know Python.

Lets look at the following use case.

It could be anything and not just logs.

All the methods return time in seconds since the epoch.

you might find more details about the epoch here.

We will use a method calledos.walk(path)for traversing through the subfolders of a folder.

Lets see the code in detail.

you gotta adjust the following two variables in the above code based on the requirement.

It is similar to the above script.

Adjust the following two variables.

We can find the extension of a file using theos.path.splitext(path)method.

It returns a tuple containing the path and the extension of the file.

Dont forget to update the path and extension variable in the above code to meet your requirements.

I would suggest testing the scripts in the NON PRODUCTION environment.