import os import sys import time import argparse def get_suite_option(): parser = argparse.ArgumentParser(description="Tsg diagnose Tools - clear tsg diagnose result file", epilog = "Example:help") parser.add_argument('-t','--timeout', type = int, default = 604800,help='Specify the time to delete files that are not accessed in seconds ,the default is 604800 (7 days)') parser.add_argument('-d','--dictpath', type = str, default = '/root/result_tsg_diagnose/unittest', help='Specify the folder to delete files, the default is /root/result_tsg_diagnose/unittest') args = parser.parse_args() return args def clear_dict_file(dictpath, timeout): try: files = os.listdir(dictpath) if not files: print("The directory not exist, the process will exit") sys.exit(0) for file in files: filePath = dictpath + "/" + file if os.path.isfile(filePath): last = int(os.stat(filePath).st_mtime) now = int(time.time()) if (now - last >= timeout): os.remove(filePath) print(filePath + " was removed!") elif os.path.isdir(filePath): clear_dict_file(filePath,timeout) if not os.listdir(filePath): os.rmdir(filePath) except Exception as ex: print("Process get an exception, will exit, Exception info: ", ex) sys.stdout.write(str(ex)) sys.exit(1) if __name__ == '__main__': args = get_suite_option() timeout = args.timeout dictpath = args.dictpath clear_dict_file(dictpath, timeout)