zhaoqingang
2025-02-06 b9c7727dc6fbb3789f063c0616ef9392311fecb2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import os.path
from datetime import datetime, timedelta
 
 
def delete_file_after_delay():
    path = "data/output"
    data_dir = os.path.dirname(path)
    if not os.path.exists(data_dir):
        return
    if not os.path.exists(path):
        return
    now = datetime.now()
    five_minutes_ago = now - timedelta(minutes=5)
    for filename in os.listdir(path):
        file_path = os.path.join(path, filename)
        if os.path.isfile(file_path):
            creation_time = os.path.getctime(file_path)
            file_creation_datetime = datetime.fromtimestamp(creation_time)
            if file_creation_datetime < five_minutes_ago:
                os.remove(file_path)
                print(f"定时删除文件: {file_path}")