You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.8 KiB
56 lines
1.8 KiB
import os
|
|
root_path = os.path.abspath(os.path.dirname(__file__)).split('telegram_crawler')[0] + "telegram_crawler"
|
|
import time
|
|
from loguru import logger
|
|
import shutil
|
|
|
|
"""
|
|
定时脚本删除目标目录下文件
|
|
暂定每日执行一次 删除一天前的文件
|
|
0 0 * * * conda activate python3.8 && cd /opt/crawl/telegram/telegram_crawler && python clean.py > /dev/null 2>&1
|
|
conda activate python3.8 && cd /opt/crawl/telegram/telegram_crawler && python clean.py > /dev/null 2>&1
|
|
"""
|
|
|
|
|
|
def release_resources(days=3):
|
|
"""
|
|
定时删除 资源文件
|
|
:param days:
|
|
:return:
|
|
"""
|
|
resources_dir = 'tg_module/resources'
|
|
profile_dir = 'tg_module/profile'
|
|
|
|
files = []
|
|
# 获取目录下所有文件
|
|
resources_files = [os.path.join(resources_dir, f) for f in os.listdir(resources_dir)]
|
|
profile_files = [os.path.join(profile_dir, f) for f in os.listdir(profile_dir)]
|
|
files.extend(resources_files)
|
|
files.extend(profile_files)
|
|
now = time.time()
|
|
delay = days * 24 * 60 * 60
|
|
delay = days * 60 * 60
|
|
|
|
logger.info(f"获取数量共:{len(files)};resources: {len(resources_files)}, profile: {len(profile_files)}")
|
|
limited_time = now - delay
|
|
|
|
# 按照创建时间排序
|
|
files = sorted(files, key=os.path.getctime)
|
|
count = 0
|
|
for i in files:
|
|
create_time = os.path.getctime(i)
|
|
if create_time < limited_time: # 需要删除
|
|
if os.path.isfile(i) or os.path.islink(i): # 如果是文件则直接删除
|
|
os.remove(i)
|
|
count += 1
|
|
else: # 删除文件夹及其所有内容
|
|
shutil.rmtree(i)
|
|
logger.debug(f"删除了一个文件夹: {i}")
|
|
else:
|
|
break
|
|
|
|
logger.info(f"删除文件数量:{count}")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
release_resources()
|