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,则开始删除文件
|
|
|
|
|
# 获取当前文件夹的大小
|
2023-12-14 14:21:17 +08:00
|
|
|
|
try:
|
|
|
|
|
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))
|
|
|
|
|
# print("删除文件" + oldest_file)
|
|
|
|
|
else:
|
|
|
|
|
# print("文件夹小于20G")
|
|
|
|
|
break
|
|
|
|
|
time.sleep(2)
|
|
|
|
|
except:
|
|
|
|
|
pass
|