2023-12-11 11:05:05 +08:00
|
|
|
|
import os
|
|
|
|
|
import glob
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
# 设置您需要监控的文件夹路径
|
|
|
|
|
folder_path = '/home/lihai/pi/mp4'
|
|
|
|
|
# 删除的达到大小 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))
|
|
|
|
|
else:
|
2023-12-11 11:05:46 +08:00
|
|
|
|
print("文件夹小于20G")
|
2023-12-11 11:05:05 +08:00
|
|
|
|
break
|
|
|
|
|
# 等待5秒钟
|
|
|
|
|
time.sleep(5)
|