六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 43|回复: 0

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

[复制链接]

升级  92%

10

主题

10

主题

10

主题

童生

Rank: 1

积分
46
 楼主| 发表于 2013-1-28 19:17:03 | 显示全部楼层 |阅读模式
这几天上班说让用个敏感词桌面软件查出带有敏感词的文件,删掉,怕被外面窃取情报。差了一大堆完全不沾边的Licence,说是应付检查,最好也删掉,可那软件只是打出一大堆日志,完全不管清理,只能自己一个个粘贴地址删除。于是便打算用python写个小程序,从日志里找出文件路径自己删除,话不多说,直接上代码.
import re,osf = file('log.txt')filenamelist = []exitflag = False# find the files from log.txtwhile 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 filenamef.close()# delete the fileswhile 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
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表