Automate log file clearing

Automate log file clearing

Repetitive daily activities can get cumbersome and tiring. Recently, I found myself trying to navigate through all the folders in my personal computer searching for log files and deleting them one after the other. This process is tiring. For this reason, I wrote a python scripts that does all my log clearing.

Before we go on, if you are reading this and have always wondered what webscrapping is and want to learn how to scrape a website, my article on 5 steps to easy webscraping is for you. Check it out!

The solution implemented in the article can be found here: Github repo

Tools used

  1. Python programming language

Packages used

  1. Os.path : to access directories and files
  2. Datetime: to create instance of data and time the files were last modified
  3. Beepy: User notification alert

Solution process

To effectively solve the problems, every steps that is to be executed is functionalized. I will explain each function in this section. If you would rather skip the explanation and review the implemented solution, you can find the github link here.

  1. success_alert: to alert the user if the task was completed successfully
    def success_alert():
     beepy.beep(6)
    
  2. error_alert: to alert the user if there was an error in execution
    def error_alert():
     beepy.beep(2
    
  3. check_path: to check if the path specified by the user exists on the personal computer
    # create a function to check if log_directory path exists
    def check_path(path):
     if os.path.exists(path):
         print("file path exists")
     else:
         error_alert()
         print(f'{path} does not exist')
    
  4. confirm_directory: to check if the existing path is a directory or not. This is because the scripts require a directory in which it loops the directory to select specific files
    # create function to check if path is a directory
    def confirm_directory(path):
     if os.path.isdir(path):
         print(f'{path} is a directory, we are good to go')
     else:
         error_alert()
         print(f'{path} is not a directory, change path and try again')
    
  5. create_file_path: check for sub folders and iterate through them to create file_path and delete all log files created today.

    # check for sub folders and iterate through them to create file_path and delete all log files created today
    def create_file_path(path):
     for base_folder, folder, files in os.walk(path):
         # check for files
         for file in files:
             # create file_path
             file_path = os.path.join(base_folder, file)
    
             # getting file extension
             file_extension = os.path.splitext(file_path)[1]
    
             # compare file extension with log_extension
             if log_extension == file_extension:
                 # check file properties for set condition
                 # get date file was last modified
                 timestamp = date.fromtimestamp(path.stat().st_ctime)
    
                 if date.today() == timestamp:
                     if not os.remove(file_path):
                         success_alert()
                         print(f'{file_path} removed successfully')
                     else:
                         print(f'Unable to delete {file_path}')
             else:
                 print(f'{file_path} is not a log file')
    

Usage: to make use of the solution, simply download and install python from this link, run pip install requirements.txt and run the python scripts available in the github repo provided here.

Furthermore: Feel free to contribute your own python utility scripts to this repo, lets build a community together. I look forward to your pull request!.