您的位置:首页 > 编程语言 > Python开发

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

2010-04-07 22:07 1141 查看
#通过分析两次的扫描日志,来分析文件的,增,删,改的记录,把分析的结果导入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...'

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/liyzh_inspur/archive/2009/02/20/3913991.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐