python删除指定后缀文件


哈哈哈...

直接上代码

开始-----------------------------------------------------------------------------

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
import os
import time


def batch_del_files(path_name, suffix, day):
'''
这是一个删除特定后缀文件的func;
:param path_name: 绝对路径名字;
:param suffix: 删除文件的后缀名;
:param day: 需要删除多少天之前的文件;
:return: 无返回值;
'''
# time_s = day * 24 * 60 * 60
time_s = 60
time_now = time.time()
suffix_hint = 0
day_hint = 0
for f in os.listdir(path_name):
if f.endswith(suffix):
suffix_hint += 1
path_file = os.path.join(path_name, f)
if os.stat(path_file).st_ctime < time_now - time_s:
os.remove(path_file)
day_hint += 1
if suffix_hint == 0:
print('\033[0;31m注意:没有后缀为%s的文件...\033[0m' % suffix)
else:
if day_hint == 0:
print('\033[0;31m注意:路径为%s的文件夹,没有大于%s天的文件...\033[0m' % (path_name, day))
else:
print('''
删除文件完成!!!
共计删除%s个,后缀为%s的文件...
''' % (day_hint, suffix))

batch_del_files(path_name='/home/test/test', suffix='txt', day=30)

代码结束-----------------------------------------------------------------------------