Automatically delete old python log files

When working with Python, it is common to generate log files to track the execution of a program. However, over time, these log files can accumulate and take up unnecessary disk space. To avoid this, it is important to regularly delete old log files. In this article, we will explore three different ways to automatically delete old Python log files.

Option 1: Using the os module

The first option is to use the os module in Python to delete old log files. The os module provides a function called remove() that can be used to delete a file. To delete old log files, we can use the listdir() function to get a list of all files in a directory, and then iterate over the list to check the file creation date. If the file is older than a certain threshold, we can use the remove() function to delete it.


import os
import time

def delete_old_log_files(directory, threshold_days):
    current_time = time.time()
    threshold_time = current_time - (threshold_days * 24 * 60 * 60)

    for file in os.listdir(directory):
        file_path = os.path.join(directory, file)
        if os.path.isfile(file_path):
            creation_time = os.path.getctime(file_path)
            if creation_time < threshold_time:
                os.remove(file_path)

Option 2: Using the glob module

The second option is to use the glob module in Python to find and delete old log files. The glob module provides a function called glob() that can be used to search for files using wildcards. We can use this function to find all log files in a directory and then iterate over the list to check the file creation date. If the file is older than a certain threshold, we can use the os.remove() function to delete it.


import glob
import os
import time

def delete_old_log_files(directory, threshold_days):
    current_time = time.time()
    threshold_time = current_time - (threshold_days * 24 * 60 * 60)

    log_files = glob.glob(os.path.join(directory, "*.log"))
    for file_path in log_files:
        creation_time = os.path.getctime(file_path)
        if creation_time < threshold_time:
            os.remove(file_path)

Option 3: Using the pathlib module

The third option is to use the pathlib module in Python to delete old log files. The pathlib module provides an object-oriented approach to working with file paths. We can use the Path() function to create a Path object for a directory, and then use the glob() method to find all log files in the directory. We can iterate over the list of log files and check the file creation date. If the file is older than a certain threshold, we can use the unlink() method to delete it.


import pathlib
import time

def delete_old_log_files(directory, threshold_days):
    current_time = time.time()
    threshold_time = current_time - (threshold_days * 24 * 60 * 60)

    log_files = pathlib.Path(directory).glob("*.log")
    for file_path in log_files:
        creation_time = file_path.stat().st_ctime
        if creation_time < threshold_time:
            file_path.unlink()

After exploring these three options, it is clear that the third option using the pathlib module is the most elegant and Pythonic way to delete old log files. It provides a clean and concise syntax, making the code easier to read and maintain. Additionally, the pathlib module is part of the Python standard library, so there is no need to install any external dependencies. Therefore, option 3 is the recommended approach for automatically deleting old Python log files.

Rate this post

6 Responses

  1. Option 1 seems like a classic choice, but Option 3 with pathlib brings something fresh to the table. Thoughts?

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents