def clean_file_with_day(target_path: str, ext: str, expire_day: int = 7) -> int:
"""
遍历目录,删除里面过期的文件指定后缀文件
- param target_path :{str} 要遍历的目录
- param ext :{str} 要遍历的文件
- param expire_day=7 :{int} 过期日期
@returns `{int}` 返回删除了多少个文件
"""
if ext.startswith("."):
ext = ext[1:]
count = 0
currt_time = time.time()
for each in pathlib.Path(target_path).iterdir():
if not each.name.endswith(ext):
continue
file_path = f"{target_path}/{each.name}"
file_info = os.stat(file_path)
try:
res = time_compare_time(currt_time, file_info.st_ctime)
if res > expire_day:
os.remove(file_path)
count += 1
except:
continue
return count