-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (42 loc) · 1.72 KB
/
main.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
"""Script to remove old backups"""
import os
import configparser
import sys
from datetime import datetime, timedelta
def remove_backups(path, days_since_mod, minimum_size, file_extension):
"""Remove old backups from path"""
treshold = datetime.now() - (timedelta(days=days_since_mod))
try:
entries = os.listdir(path)
except FileNotFoundError:
print("The path:", path, "could not be found")
sys.exit(1)
for file in entries:
# Get modification time from file
mod_time = datetime.fromtimestamp(os.stat(os.path.join(path, file)).st_mtime)
# Get filesize of file
file_size = os.stat(os.path.join(path, file)).st_size
for extension in file_extension:
# Check if file has extension to be removed.
if os.path.join(path, file).endswith(extension):
if mod_time < treshold and file_size > minimum_size:
os.remove(os.path.join(path, file))
print(file, "has been removed")
if __name__ == "__main__":
config = configparser.ConfigParser()
config.read("config.ini")
for section in config.sections():
try:
data = dict(config.items(section))
except KeyError:
print("No sections in the config file")
sys.exit(1)
try:
filepath = data["path"]
last_modified = data["last_modified"]
min_size = data["size"]
file_ext = data["file_extensions"].replace(" ","").split(",")
except KeyError as key:
print(key, "could not be found in the", section, "section of the config.")
sys.exit(1)
remove_backups(filepath, int(last_modified), int(min_size), file_ext)