linux 通过python 命令定时删除文件,不通过crontab
import os import time log_dir = "/var/log/" def delete_expired_logs(): current_time = time.localtime() print(current_time.tm_hour) if current_time.tm_hour == 12 and current_time.tm_min == 30: print('xxxxxxxxxx') for filename in os.listdir(log_dir): print(filename ,'filename') print(time.time()) if filename.startswith('messages-202') or filename.startswith('secure-202') : print(filename) file_path = os.path.join(log_dir, filename) os.remove(file_path) print("22") with open('D:\sdm\X.txt', 'a') as file: file.write( T + ' ' +filename + ' 被删除,删除脚本存放在了 /var/log目录下了,通过后台nohup python del_log 命令运行的.' + '\n') while True: delete_expired_logs() time.sleep(10)