Last active
April 26, 2020 15:47
-
-
Save ctvanzandt42/3b32d8ab3de9c583c28bda229005697f to your computer and use it in GitHub Desktop.
Log Cleanup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python log_cleanup.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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