|
楼主 |
发表于 2020-7-17 09:55:44
|
显示全部楼层
刚开始学,照着网上拼凑的,
import os
import easygui as a
# -*- coding: utf-8 -*-
class SearchFile(object):
def findfile(self, keyword, workpath):
filelist = [] # 文件路径
filelist_ones = [] # 绝对路径
file_qjs = [] # 清机
file_xfs = [] # 信封
file_cks = [] # 存款
file_gzs = [] # 故障
file_xlhs = [] # 序列号
for workpath, dirs, files in os.walk(workpath):
for name in files:
fitfile = filelist.append(os.path.join(workpath, name))
# print(fitfile)
# print(os.path.join(workpath, name))
# print(filelist)
# print('...........................................')
for i in filelist:
if os.path.isfile(i):
# print(i)
if keyword in os.path.split(i)[1]:
# print(i) # 绝对路径
filelist_one = filelist_ones.append(i)
# else:
# print('......no keyword!')
# print(filelist_ones)
# print('...........................................')
for filelist_one in filelist_ones:
with open(filelist_one, 'r', encoding='utf-8') as f_obj:
# print("清机信息")
for line in f_obj:
if '清机:' in line: # 查找清机日志行
# print(line)
file_qj = file_qjs.append(line)
elif 'totalAmount:' in line: # 查找现金存款日志行
file_ck = file_cks.append(line)
elif '手输金额:' in line: # 查找信封日志行
# print(line)
file_xf = file_xfs.append(line)
elif '机芯故障代码:' in line: # 查找机芯故障日志行
# print(line)
file_gz = file_gzs.append(line)
elif '设备序列号:' in line: # 查找设备序列号
file_xlh = file_xlhs.append(line[-9:])
print("设备序列号:"+file_xlhs[0])
print("清机信息")
for file_qj in file_qjs:
print(file_qj)
print('...........................................')
print("故障信息")
for file_gz in file_gzs:
print(file_gz)
print('...........................................')
print('现金存款')
for file_ck in file_cks:
print(file_ck)
print('...........................................')
print('信封存款')
for file_xf in file_xfs:
print(file_xf)
print('...........................................')
filename = '分析结果.txt'
with open(filename, 'w', encoding='utf-8') as file_ob:
file_ob.write("设备序列号:"+file_xlhs[0]+'\n')
file_ob.write('清机信息:' + '\n')
for len_1 in range(len(file_qjs)):
# ss =str(file_gzs[len_1]).replace('[','').replace(']','')
qj = str(file_qjs[len_1]).replace(',', '') + '\n'
file_ob.write(qj)
file_ob.write('故障信息:' + '\n')
for len_2 in range(len(file_gzs)):
gz = str(file_gzs[len_2]).replace(',', '') + '\n'
file_ob.write(gz)
file_ob.write('现金存款:' + '\n')
for len_3 in range(len(file_cks)):
ck = str(file_cks[len_3]).replace(',', '') + '\n'
file_ob.write(ck)
file_ob.write('信封存款:' + '\n')
for len_4 in range(len(file_xfs)):
xf = str(file_xfs[len_4]).replace(',', '') + '\n'
file_ob.write(xf)
file_ob.close()
def __call__(self):
while True:
workpath = ret
if (workpath == ''):
break
# if workpath=='y' or workpath=='Y':
# root=self.abspath # 把当前工作目录作为工作目录
# print('当前工作目录:',root)
# dirlist=os.listdir() # 列出工作目录下的文件和目录
# print(dirlist)
else:
# root=input('please enter the working directory:')
print('当前工作目录:', workpath)
# keyword=input('the keyword you want to find:')
# if(keyword==''):
# break
self.findfile('XZ_JS', workpath) # 查找带指定字符的文件
break
ret = a.enterbox(msg='请输入文件路径', title='日志处理', strip=True)
print(ret)
if __name__ == '__main__':
search = SearchFile()
search()
|
|