用python在日志中查找文件路径并删除文件

这几天上班说让用个敏感词桌面软件查出带有敏感词的文件,删掉,怕被外面窃取情报。差了一大堆完全不沾边的Licence,说是应付检查,最好也删掉,可那软件只是打出一大堆日志,完全不管清理,只能自己一个个粘贴地址删除。于是便打算用python写个小程序,从日志里找出文件路径自己删除,话不多说,直接上代码.

import re,os
f = file('log.txt')
filenamelist = []
exitflag = False

# find the files from log.txt
while True:
    line = f.readline()
    if len(line) == 0:
        break
    filenames = re.split('  ',line)
    
    for filename in filenames:
        if re.match(r'^[A-Za-z]\:\\[^\:\?\"\>\<\*]*\.(doc|xls|ppt|pdf|txt|rtf|docx|xlsx|pptx|pps)$', filename):
            filenamelist.append(filename)
            print filename
f.close()

# delete the files
while not exitflag:
    confirm = raw_input('Are you insure deleting them?(Y/N)')
    if confirm == 'Y' or confirm == 'y':
        for filename in filenamelist:
            if os.path.isfile(filename): #judge whether the file exists                
                os.remove(filename)
                print '%s is deleted' % filename
        exitflag = True    
    elif confirm == 'N' or confirm =='n':
        exitflag = True
    else:
        exitflag = False

你可能感兴趣的:(python,F#,OS)