1、根据传入的参数,文件所在目录,匹配文件的正则表达式,过期天数进行删除,这些可写在配置文件del_file.conf。
del_file3.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #!/usr/bin/env python # encoding: GBK import os import re import sys import time import datetime import logging import shutil #reload(sys) #sys.setdefaultencoding('utf-8') logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s %(filename)s[line:%(lineno)-1d] %(levelname)s %(message)s' , datefmt = '%a, %d %b %Y %H:%M:%S' , filename = 'myapp.log' , filemode = 'a' ) # logging.debug('This is debug message') # logging.info('This is info message') # logging.warning('This is warning message') def find_file(file_dir, file_re = '\d{4}-\d{2}-\d{2}' , expire_time = 7 ): # print sys.getdefaultencoding() if file_re = = '': logging.error( 'file_re is null,exit' ) return None #解决编码问题 #file_dir = file_dir.decode("utf-8") #file_re = file_re.decode("utf-8") logging.info( '传入参数 :目录 [%s],正则表达式[%s],过期天数 [%s]' % (file_dir,file_re,expire_time)) #目录下所有文件 all_file = os.listdir(file_dir) #匹配正则的文件 reg_file_list = [] reg_str = file_re for reg_file in all_file: if re.match(reg_str,reg_file): logging.info( '正则匹配到文件:[%s]' % reg_file) reg_file_list.append(reg_file) if len (reg_file_list) < = 7 : logging.info( '匹配文件数小于7个,不进行删除操作!' ) return None #满足过期时间的文件 #当前时间 today = datetime.datetime.now() #n天 n_days = datetime.timedelta(days = int (expire_time)) #n天前日期 n_days_agos = today - n_days #n天前时间戳 n_days_agos_timestamps = time.mktime(n_days_agos.timetuple()) for date_file in reg_file_list: abs_file = os.path.join(file_dir,date_file) file_timestamp = os.path.getmtime(abs_file) if float (file_timestamp) < = float (n_days_agos_timestamps): logging.info( '过期匹配到文件:[%s]' % abs_file) #print "匹配到文件:" ,abs_file #返回满足条件的文件 if os.path.isfile(abs_file): os.remove(abs_file) logging.info( '删除文件:[%s]成功' % abs_file) if os.path.isdir(abs_file): shutil.rmtree(abs_file) logging.info( '删除目录:[%s]成功' % abs_file) def read_conf(file_path): with open (file_path, 'r' ) as f: for line in f: line_list = line.strip().split( ',' ) if len (line_list) ! = 3 : logging.warning( '%s 行配置不正确' % line.strip()) continue file_dir = line_list[ 0 ] file_re = line_list[ 1 ] expire_time = line_list[ 2 ] find_file(file_dir,file_re,expire_time) if __name__ = = "__main__" : read_conf(sys.argv[ 1 ]) |
del_file.conf
1 | C:\Users\Administrator\Desktop\Python学习\Python测试目录,. * 数据, 30 |
2 、定期整理日期文件或文件夹,传入参数:文件夹所在目录,匹配文件夹的正则表达式,整理多少天的文件夹,参数可写在配置文件dir_reg.conf。
move_file.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #!/usr/bin/env python # encoding: GBK import os import re import sys import time import datetime import logging import shutil #reload(sys) #sys.setdefaultencoding('utf-8') logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s %(filename)s[line:%(lineno)-1d] %(levelname)s %(message)s' , datefmt = '%a, %d %b %Y %H:%M:%S' , filename = 'D:\\move.log' , filemode = 'a' ) # logging.debug('This is debug message') # logging.info('This is info message') # logging.warning('This is warning message') def find_file(file_dir, file_re = '数据' , expire_time = 60 ): logging.info( '传入参数 :目录 [%s],正则表达式[%s],过期天数 [%s]' % (file_dir,file_re,expire_time)) if not os.path.exists(file_dir): logging.info( '传入参数 :目录 [%s]不存在' % file_dir) return None #匹配文件或目录 #目录下所有文件 all_file = os.listdir(file_dir) #匹配正则的文件或目录 reg_file_list = [] reg_str = file_re for reg_file in all_file: #if os.path.isdir(reg_file): # continue if re.match(reg_str,reg_file): logging.info( '正则匹配到文件:[%s]' % reg_file) reg_file_list.append(reg_file) if len (reg_file_list) < 7 : logging.info( '匹配文件数小于7个,不进行移动操作!' ) return None #满足过期时间的文件 #当前时间 today = datetime.datetime.now() #1天前时间 one_days = datetime.timedelta(days = 1 ) one_days_agos = today - one_days #1天前时间文件夹 one_days_agos_dir = one_days_agos.strftime( "%Y-%m-%d" ) #1天前时间戳 one_days_agos_timestamps = time.mktime(one_days_agos.timetuple()) #n天前时间 n_days = datetime.timedelta(days = int (expire_time)) n_days_agos = today - n_days #n天前时间文件夹 n_days_dir = n_days_agos.strftime( "%Y-%m-%d" ) #n天前时间戳 n_days_agos_timestamps = time.mktime(n_days_agos.timetuple()) #新建目录000-00-00~0000-00-00 date_dir = '%s_%s' % (n_days_dir,one_days_agos_dir) if not os.path.exists(os.path.join(file_dir,date_dir)): os.mkdir(os.path.join(file_dir,date_dir)) #移动1~n天期间的文件或目录 for date_file in reg_file_list: abs_file = os.path.join(file_dir,date_file) file_timestamp = os.path.getctime(abs_file) if float (n_days_agos_timestamps) < = float (file_timestamp) < = float (one_days_agos_timestamps): logging.info( '移动文件:[%s]' % abs_file) #print "匹配到文件:" ,abs_file #移动满足条件的文件 shutil.move(abs_file, os.path.join(file_dir,date_dir)) logging.info( '移动:[%s]到[%s]成功' % (abs_file,os.path.join(file_dir,date_dir))) def read_conf(file_path): with open (file_path, 'r' ) as f: for line in f: line_list = line.strip().split( ',' ) if len (line_list) ! = 3 : logging.warning( '%s 行配置不正确' % line.strip()) continue file_dir = line_list[ 0 ] file_re = line_list[ 1 ] expire_time = line_list[ 2 ] find_file(file_dir,file_re,expire_time) if __name__ = = "__main__" : read_conf(sys.argv[ 1 ]) |
dir_reg.conf
1 2 | D:\mylog,^\d{ 4 } - \d{ 2 } - \d{ 2 }$, 30 D:\mylog,^\d{ 4 } - \d{ 2 } - \d{ 2 }$, 90 |
分类:
Python
标签:
python整理文件
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2016-09-07 Oracle:shared memory realm does not exist
2016-09-07 Oracle判断语句集合(转载)