-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_old.py
52 lines (30 loc) · 1.21 KB
/
delete_old.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
import os
import re
import datetime
LIMIT = 14
path = '/home/steam/Backups/Valheim'
now = datetime.datetime.now()
regex = re.compile(r'(20\d\d)-(\d\d)-(\d\d)_(\d\d)-(\d\d)-(\d\d)')
matches = []
mbytes_deleted = 0
count = 0
for file in os.listdir(path):
if regex.search(file):
match = regex.search(file)
year = int(match.group(1))
month = int(match.group(2))
day = int(match.group(3))
hour = int(match.group(4))
minute = int(match.group(5))
second = int(match.group(6))
file_date = datetime.datetime(year, month, day, hour=hour, minute=minute, second=second)
difference = now - file_date
if difference.days > LIMIT:
count += 1
file_to_delete = os.path.join(path, file)
if os.path.exists(file_to_delete):
mbytes_deleted += os.stat(file_to_delete).st_size / 1000000 # Get size of file and convert to Megabytes
print(file_to_delete + ' deleted.')
os.remove(file_to_delete)
mbytes_deleted = round(mbytes_deleted)
print(f'{count} files deleted ({mbytes_deleted}MB)')