lot_manager/delete_than20G.py

28 lines
1003 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import glob
import time
# 设置您需要监控的文件夹路径
folder_path = '/home/pi/mp4'
# 删除的达到大小 20G 就一直删除
size = 20
# 检查文件夹大小如果超过20G则开始删除文件
# 获取当前文件夹的大小
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