# -*- coding:utf-8 -*- # import os import time import sys def del_expire_file(file_path,expire_day): if not os.path.isdir(file_path): print("{} is not exist".format(file_path)) sys.exit(2) expire_time = time.time() - expire_day * 24 * 3600 os.chdir(file_path) for file_name in os.listdir(file_path): if os.path.isfile(file_name): if os.path.getmtime(file_name) < expire_time: #正式执行前先屏蔽remove,print输出观察是否符合预期,避免误删除 print("need remove {}".format(os.path.join(file_path,file_name))) #os.remove(file_name) if __name__=='__main__': file_path = r'/home/xiaodongl/test' expire_day = 1 del_expire_file(file_path,expire_day)