python定时扫描文件夹、多线程、日志输出、文件备份
代码展示
import _thread
import time
import shutil
import datetime as dt
import logging
import os
from threading import Timer
import datetime
import _thread
import time
import shutil
from xml.dom.minidom import parse
import datetime as dt
import os
import logginglogger = logging.getLogger()
logger.setLevel(logging.INFO)rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
log_path = os.path.dirname(os.getcwd()) + '/opt/data/log/dycktozslogs/'
log_name = log_path + rq + '.log'
logfile = log_name
fh = logging.FileHandler(logfile, mode='w')
fh.setLevel(logging.DEBUG)formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)logger.addHandler(fh)
def copy_remove_file(srcpath):time.sleep(6)try:global domTreetry:domTree = parse(srcpath)except BaseException:logger.error("xml文件解释错误:" + srcpath)returnrootNode = domTree.documentElementSenderId = rootNode.getElementsByTagName("dxp:SenderId")if SenderId.length > 0:data = SenderId[0].childNodes[0].datayear = dt.datetime.now().strftime('%G')month = dt.datetime.now().strftime('%m')day = dt.datetime.now().strftime('%d')path = os.path.join("/opt/data/backup/", data)path = os.path.join(path, year)path = os.path.join(path, month)path = os.path.join(path, day)if os.path.exists(path):try:shutil.copy(srcpath, path)except shutil.Error:logger.error("备份文件出错,文件被占用:" + srcpath)returnelse:try:os.makedirs(path)except Exception:logger.error("当文件已存在,无法创建该文件:" + path + "--报文路径:" + srcpath)finally:try:shutil.copy(srcpath, path)except shutil.Error:logger.error("备份文件出错,文件被占用:" + srcpath)returntry:zspath = os.path.join("/opt/data/component/send", data)if os.path.exists(zspath):shutil.move(srcpath, zspath)else:try:os.makedirs(zspath)except Exception:logger.error("当文件已存在,无法创建该文件:" + path + "--报文路径:" + srcpath)finally:shutil.move(srcpath, zspath)except shutil.Error:logger.error("移动文件出错,文件已经存在:" + srcpath)returnlogger.info("增加文件的文件夹路径:" + srcpath)else:logger.error("报文格式不正确:" + srcpath)except Exception as err:logger.error("程序运行报错了:" + srcpath + ",报错内容:" + err)returndef initTask():print('TimeNow:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))for root, dirs, files in os.walk("/opt/data/component/dyck/fromDYCK"):for file in files:_thread.start_new_thread(copy_remove_file, (os.path.join(root, file),))print(os.path.join(root, file))t = Timer(3, initTask)t.start()if __name__ == "__main__":initTask()