文件夹下匹配条件删除文件的python2.x脚本

随笔写的,没有用函数之类的,懒得想了(而且我入门阶段,门槛都没摸到)


#!/usr/bin/env python

#coding=utf-8

'''删除有哈哈字样的文件'''

import os,time

file_list = []
rootdir = '/root/haha'
list = os.listdir(rootdir) 
for i in range(0,len(list)):
    path = os.path.join(rootdir,list[i])
    if os.path.isfile(path):
        file_list.append(path)
    else:
        pass

a = u'哈哈'
b = a.encode('utf-8')

for filename in file_list:
    file_name = filename.decode('utf-8')
    jiechu = file_name[-20:-18]
    if jiechu == b.decode('utf-8'):
        timestamp = int(os.path.getmtime(filename))
        time_differ = int(time.time()) - int(os.path.getmtime(filename))
        if time_differ > 60*60*3:
            os.system('rm -rf %s'%filename)
    else:
         pass


猜你喜欢

转载自blog.csdn.net/iris_csdn/article/details/80654831