lot_manager/delete_than20G.py

25 lines
925 B
Python
Raw Normal View History

2023-12-11 11:05:05 +08:00
import os
import glob
import time
# 设置您需要监控的文件夹路径
2023-12-11 11:08:16 +08:00
folder_path = '/home/pi/mp4'
2023-12-11 11:05:05 +08:00
# 删除的达到大小 20G 就一直删除
size = 20
# 检查文件夹大小如果超过20G则开始删除文件
# 获取当前文件夹的大小
while True:
folder_size = sum(os.path.getsize(f) for f in glob.glob(os.path.join(folder_path, '*')))
if folder_size > size * 1024 * 1024 * 1024: # 检查是否超过20G
# 获取所有文件的时间戳和文件名
files_info = [(os.path.getmtime(os.path.join(folder_path, f)), f) for f in os.listdir(folder_path)]
# 按时间戳排序,取最旧的文件
oldest_file = min(files_info, key=lambda x: x[0])[1]
# 删除最旧的文件
os.remove(os.path.join(folder_path, oldest_file))
2023-12-12 11:45:26 +08:00
# print("删除文件" + oldest_file)
2023-12-11 11:05:05 +08:00
else:
2023-12-11 11:11:13 +08:00
# print("文件夹小于20G")
2023-12-11 11:05:05 +08:00
break
2023-12-12 11:45:26 +08:00
time.sleep(2)