#!/usr/bin/python#coding=gbkimport timeimport osimport linecacheclass convert_time(object): def __init__(self,user): self.user = user def date_comm(self): file = [ x.split(' ')[-2] for x in linecache.getlines(user) if x.split(' ')[-2] != '00:00:00' and not x.startswith('User') and not x.startswith('---') ] file_time= [] for x in file: if x.find('-') == -1: file_time.append('0-%s' % x) else: file_time.append(x) return file_time def date_convert(self): 'Get the user time, generate a dictionary' datestr = self.date_comm() time_list = { 'days':0,'hours':0,'minute':0,'second':0 } for x in xrange(len(datestr)): time = datestr[x].split('-') time_l = time[1].split(':') time_list['days'] += float(time[0]) time_list['hours'] += float(time_l[0]) time_list['minute'] += float(time_l[1]) time_list['second'] += float(time_l[2]) return time_list def convert_hours(self): 'Converts time into hours' time_all = self.date_convert() hours_total = time_all['days'] * 24 + time_all['hours'] + round(time_all['minute'] / 60.0) + round(time_all['second'] / 120.0) return hours_total def convert_minute(self): 'Converts time into minutes' time_all = self.date_convert() min_total = time_all['days'] * 24 * 60 + time_all['hours'] * 60 + time_all['minute'] + round(time_all['second'] / 60) return min_total def convert_second(self): 'Converts time into second' time_all = self.date_convert() sec_total = time_all['days'] * 24 * 120 + time_all['hours'] * 120 + time_all['minute'] + time_all['second'] return sec_totalos.system('color a')print ' 用户机时统计系统'print '\n'user = raw_input('请输入文件的路径:')while True: if os.path.isfile(user): ct = convert_time(user) print """ -------------- 用户使用机时如下:--------------------- 总计:%s 小时 %s 分钟 %s 秒 ------------------------------------------------------ """ % (ct.convert_hours(),ct.convert_minute(),ct.convert_second()) list = raw_input('回车退出程序') break else: print '文件不存在,请输入一个正确的文件路径!' user = raw_input('请输入文件的路径:')