-
Notifications
You must be signed in to change notification settings - Fork 1
/
cleaner.py
42 lines (33 loc) 路 1.24 KB
/
cleaner.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
import os
import shutil
from time import sleep
from loggers import error_logger, info_logger
DIRECTORY_PATH = 'temp'
TIME = 60 * 30 # 30 minutes
def clear_directory():
# Start log
info_logger.info('Cleaner started!')
while True:
try:
# Running log
info_logger.info('Cleaning...')
# Check if the directory exists
if os.path.exists(DIRECTORY_PATH):
# Iterate over all items in the directory
for item in os.listdir(DIRECTORY_PATH):
item_path = os.path.join(DIRECTORY_PATH, item)
# Check if it is a file or a directory
if os.path.isfile(item_path):
# Remove the file
os.remove(item_path)
elif os.path.isdir(item_path):
# Remove the directory and its contents recursively
shutil.rmtree(item_path)
# Success log
info_logger.info('Successfully cleaned!')
except Exception as e:
# Handle error
error_logger.exception(f"An error occurred: {str(e)}")
info_logger.info('Cleanning finished!')
# Wait
sleep(TIME)