需求:
1、因工作需要,需要统计共享盘中指定路径的文件(照片,可以根据需求进行修改文件类型)中的数量;
2、可以按照搜索的时间段进行搜索
3、缺省按照当前时间段内落入的工作时间班次进行统计
代码如下:
# -*- coding: UTF8 -*-
'''
Create by shumoaihaozhelianmeng 20190311
Version:1.0.1
function:
1.input file path which can support sharepath
2.input search period
3.can judge morning or evening
4.count pictures number
'''
import time
import datetime
import os
def schedule():
while True:
starttime = input('请输入起始日期 格式(201903090630):')
endtime = input('请输入结束日期 格式(201903091800):')
nowtime = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
now = datetime.datetime.now()
delta = datetime.timedelta(days = 1)
# 获取当前时间落入的班次
morning = nowtime[0:8] + '0630'
shift = nowtime[0:8] + '1800'
# endtime会跨天及可能会跨月
n_days = now + delta
evening = n_days.strftime('%Y%m%d') + '0630'
if (starttime=='')&(endtime==''):
if compare_time(nowtime,morning)&(compare_time(nowtime,shift)==0):
print('搜索的数据为 早班 信息!')
starttime = morning
endtime = shift
break
elif compare_time(nowtime,shift)&(compare_time(nowtime,evening)==0):
print('搜索的数据为 中班 信息!')
starttime = shift
endtime = evening
break
else:
print('非正常工作班次信息,请重新输入!')
elif (starttime!='')&(endtime==''):
endtime = nowtime
break
elif (endtime!='')&(starttime==''):
starttime = nowtime
break
else:
starttime = starttime
endtime = endtime
break
return starttime,endtime
def TimeStampToTime(timestamp):
timeStruct = time.localtime(timestamp)
return time.strftime('%Y%m%d%H%M',timeStruct)
def get_FileModifyTime(filePath):
#filePath = unicode(filePath, 'utf8')
t = os.path.getmtime(filePath)
return TimeStampToTime(t)
def compare_time(time1,time2):
s_time = time.mktime(time.strptime(time1, '%Y%m%d%H%M'))
e_time = time.mktime(time.strptime(time2, '%Y%m%d%H%M'))
if s_time >= e_time:
return 1
else:
return 0
def get_file_extension(filename):
arr = os.path.splitext(filename)
return arr[len(arr) - 1].replace(".", "")
#return arr[len(arr) - 1].replace(".","") 可以将结果中的.去掉
def get_file_path():
sharepath = input('请输入需要访问的文件夹完整路径:')
if (sharepath==''):
sharepath = '\\192.168.56.101\MS'
else:
sharepath = sharepath+'\\'
return sharepath
if __name__ == '__main__':
sharepath = get_file_path()
starttime,endtime = schedule()
count = 0
#for filename in os.listdir('Z:\\new\\test\\'):
for filename in os.listdir(sharepath):
#print(filename)
if get_file_extension(filename) in ['jpg','JPG']:
# dt = get_FileModifyTime(
# "Z:\\new\\test\\"+filename)
dt = get_FileModifyTime(
sharepath+filename)
# filepath = sharepath+filename
# print(filepath)
# dt = get_FileModifyTime(filepath)
#print(dt)
if compare_time(dt,starttime)& (compare_time(dt,endtime)==0):
count +=1
print('在 '+starttime+' 与 '+endtime+' 期间'+' 在路径:'+sharepath+'下 照片的总数为:'+str(count)+' 张!')
os.system('pause')
使用pyinstall工具进行打包: