python文件操作(2)--分析扫描得到的日志文件把文件状态导入Excel表格

#通过分析两次的扫描日志,来分析文件的,增,删,改的记录,把分析的结果导入Excel表格。实现文件的日常管理。

import string,os


def GetList(file):#把日志文件转换为list

 

        #第一字典存放文件名后修改时间
        dict = {}


        fd = open(file,'r')


        while 1:
            line = fd.readline()
            if not line:
                break
            line = string.strip(line)


            #提取文件名,在存入文件的时候用特殊符号修饰
            str1 = line[line.find('@'):line.find('*')+1]

           

            #提取文件修改时间
            str2 = line[11:19]
            if len(line) == 0:
                continue
            dict[str1] = str2


        fd.close()
        return dict
   
if __name__ == '__main__':
    flag = ''
    f=file('result.xls','w')
    f.write('FileName  '+'/t'+'Modified time'+'/t'+'state'+'/t'+'/n')
    #todayFile =  GetList('E://poem.log')
    #yesdayFile = GetList('E://poem1.log')
    todayLog = raw_input('Input the log of today:')
    yesdayLog = raw_input('Input the log of yestoday:')
    todayFile =  GetList(todayLog)
    yesdayFile = GetList(yesdayLog)
   
    for name in todayFile:
        if name in yesdayFile:
            if todayFile[name]!=yesdayFile[name]:#文件修改过
                print name[1:-1],'Modified!'
                flag = 'Modified!'
            else:#文件保持原样
                print name[1:-1],'Ok'
                flag = 'Ok!'
                continue
        else:#新添加的文件
            print name[1:-1],'Insert!'
            flag = 'Insert!'
        f.write(name[1:-1]+'/t'+todayFile[name]+'/t'+flag+'/t'+'/n')
    for name in yesdayFile:
        if name  in todayFile:
            continue
        else:#删除的文件
            print name[1:-1], 'Delete!'
            flag = 'Delete!'
            f.write(name[1:-1]+'/t'+yesdayFile[name]+'/t'+flag+'/t'+'/n')
    print 'Press any key to exit...'

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值