import os import time # 获取当前时间 now = time.time() # 获取 1 天前的日期 one_day_ago = now - 86400 # 获取要删除的文件夹路径 directory_path = "C:\\Users\\user\\Desktop\\files" # 获取该文件夹下所有文件的创建时间和路径 files = [(file, os.path.join(directory_path, file)) for file in os.listdir(directory_path)] # 删除 1 天前创建的文件 for file, file_path in files: if os.path.isfile(file_path) and os.path.getctime(file_path) < one_day_ago: os.remove(file_path) print(f"Deleted file: {file}")