Skip to content

Instantly share code, notes, and snippets.

@ctvanzandt42
Last active April 26, 2020 15:47
Show Gist options
  • Save ctvanzandt42/3b32d8ab3de9c583c28bda229005697f to your computer and use it in GitHub Desktop.
Save ctvanzandt42/3b32d8ab3de9c583c28bda229005697f to your computer and use it in GitHub Desktop.
Log Cleanup
python log_cleanup.py
"""This script is run as a batch job once a day to clean out all old log files"""
import os
import time
WORKING_DIR = os.getcwd()
def delete_old_file(file):
"""Method that will be used to delete any old files"""
file_path = WORKING_DIR + os.path.sep + file
now = time.time()
if os.stat(file_path).st_mtime < now - 7 * 86400 \
and os.path.isfile(file_path) \
and file_path.endswith('.log'):
os.remove(file_path)
print("Removed file: " + file_path)
LOG_FILES = os.listdir(WORKING_DIR)
for f in LOG_FILES:
delete_old_file(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment