Automatically clean your file system using crontab and Python in just two steps
Today I decided to write a script which will help us with managing our day to day task. You all must have got an idea through above title that this is nothing but scheduling our file deletion script with the help of crontab.
I am very lazy to delete the files once downloaded in my system and when I want a very important file I feel like I am lost in the big mess. Bill Gates has often been quoted as saying,
“I always choose a lazy person to do a hard job, because a lazy person will find an easy way to do it.”
I am not sure if Gates has even said that but above quote definitely has a hidden truth in it. Because I have found a way to delete the particular files automatically rather me deleting them manually ;)
We can achieve this in just two steps:
Create a python script which will delete the files according to the conditions(in my case I usually download many images so I used .png files extension for demonstration purpose, you can customize it according to your need)
Create a scheduler using crontab for executing this script.
Now we will focus on first step that is creating script for that below are prerequisites:
from os import listdir, remove
from os.path import isfile, join, splitext
from datetime import datetime
Note : I have used 'datetime' module for logging the execution time in log file.
After importing above modules we need to create a delete_files() function, it checks whether the component in given path(I have used 'mypath' variable which targets the folder from which files needs to be deleted) is actually a file with the help of 'isfile' because 'listdir(mypath)' will list down all the components present in the path. These components might contain a folder or a file. And then I am checking whether the file has extension '.png' with the help of 'splitext'
Once all above conditions are met we are good to delete the file using 'remove' from os module.
So we can run the script manually and it will remove the specified files but still I am inert to run this script on my own. Here comes the saviour 'crontab'.
Here are the details to set the cron for running your script which is our second step: Open your terminal window and enter below command:
crontab -l
It will list down the already present cron jobs in crontab, if you get something like below, no need to worry:
crontab: no crontab for gayatrikale
Just type below command:
crontab -e
Then press 'i' to go into vim's insert mode.(mac os by default has vim editor) In insert mode you have to enter your cronjob, in this case I wanted to run this script every Wednesday at noon. So the frequency was
0 12 * * WED
The command which you need to insert is divided in two parts: {frequency} {command}
So our command will be python3 delete_files_periodically.py
But crontab will not understand 'python3' so we need to provide exact path from where it is calling python3. You can get that by entering below command in terminal:
And also we need to give exact path of our script to execute it, so command will look like below:
Now if we need to have a log file after executing this then we can get that by appending >>logs.txt in the command
Now press 'esc' and save the changes with ':wq'
Now we are all set to not do anything ; ) If you want the complete code then please visit the gist link