-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteLogs.py
More file actions
31 lines (24 loc) · 747 Bytes
/
DeleteLogs.py
File metadata and controls
31 lines (24 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#coded by Qais Temeiza
import os
from datetime import date
import math
noOfDays = 1 #number of days
currentDate = date.today()
directory = "/PATH TO LOG FILES/"
f = open(directory+"ScriptLogs.txt", "a")
f.write("Date of deletion " + str(date.today()) + ":\n")
for fileName in os.listdir(directory):
if fileName.endswith(".log"):
noExtFileName = os.path.splitext(fileName)[0]
try:
year = noExtFileName[:4]
month = noExtFileName[4:-2]
day = noExtFileName[6:]
fileDate = date(int(year),int(month),int(day))
dateDiff = (currentDate - fileDate).days
if dateDiff >= noOfDays:
f.write(fileName + "\n")
os.remove(directory+fileName)
except Exception, e:
f.write("An error has occured:" + str(e)+"\n")
continue