-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfile_sanitizer.py
executable file
·67 lines (55 loc) · 1.86 KB
/
file_sanitizer.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
"""
Script :file_sanitizer.py
Author :Julio Sanz
Website :www.elarraydejota.com
Email :juliojosesb@gmail.com
Description :Python script to compress and remove files older than X days
Usage :file_sanitizer.py
Modify in the script the variables as needed
License :GPLv3
"""
# =======================
# MODULES IMPORTS
# =======================
import time,os,gzip
# =======================
# VARIABLES
# =======================
# Location of files to compress/delete
folder = "/path/to/log/folder/"
# Compress files older than specified
compress_older_days = 5
# Delete files older than specified
delete_older_days = 90
# Current time
now = time.time()
# =======================
# FUNCTIONS
# =======================
def sanitize_files():
# Loop through all the folder
for file in os.listdir(folder):
f = os.path.join(folder,file)
if not f.endswith('.gz'):
if os.stat(f).st_mtime < now - (60*60*24*compress_older_days) and os.path.isfile(f):
print ("...Compressing file "+f)
out_filename = f + ".gz"
f_in = open(f, 'rb')
s = f_in.read()
f_in.close()
f_out = gzip.GzipFile(out_filename, 'wb')
f_out.write(s)
f_out.close()
# Remove original uncompressed file
os.remove(f)
# We ensure that the file we are going to delete has been compressed before
if f.endswith('.gz'):
if os.stat(f).st_mtime < now - (60*60*24*delete_older_days) and os.path.isfile(f):
print ("...Deleting old file "+f)
os.remove(f)
# =======================
# MAIN
# =======================
if __name__ == '__main__':
sanitize_files()