工作需要,根据所给课程信息,从试卷库里随机抽调合适的试卷。
python真是方便,代码行数估计也就相当于vc的40%
记下来,免得自己弄掉了^_^
#! D:/Program Files/python25/python.exe
# -*- encoding:gb2312 -*-
import os,shutil,re,random,sys
okfile=0
#汇总试卷库中现有的试卷,结果保存在e:/paperInfo.txt中
#检查是否有文件paperinfo.txt(试卷库有改动,删除paperinfo.txt,程序将重新汇总)
paperCount=0
if os.path.isfile(r'e:/paperinfo.txt'):
pass
else:
print '正在汇总试卷信息,请耐心等待 ....'
paperFile=open('e:/paperinfo.txt','w')
for root,dirs,files in os.walk(r'e:/试卷库'):
for i in files:
print >>paperFile,i,',',os.path.join(root,i)
paperCount+=1
paperFile.close()
print '共汇总试卷',paperCount,'份'
#把要考的课程保存在e:/demand.txt中,程序自动检测
if os.path.isfile(r'e:/demand.txt'):
pass
else:
print '请先准备demant.txt文件'
raw_input("press any key quit!")
sys.exit()
demandFile=open(r'e:/demand.txt','r')
temdemandInfo=demandFile.readlines()
demandFile.close()
temdemandInfo1=[i.rstrip() for i in temdemandInfo]
temdemandInfo2=[i.split('/t')[5] for i in temdemandInfo1]
#重复的课程只保留一个记录
#demandInfo=[]
#t=temdemandInfo[0]
#for i in temdemandInfo:
# tempi=i.split('/t')
# tempt=t.split('/t')
# if tempi[0]==tempt[0]:
# continue
# else:
# t=i
# demandInfo.append(i)
#print newdata
demandInfo=list(set(temdemandInfo2))
print '要找文件:',len(demandInfo)
#获取试卷库中现有试卷信息
paperFile=open(r'e:/paperinfo.txt','r')
paperInfo=paperFile.readlines()
paperFile.close()
#建立e:/testpaper文件夹,存放所需试卷文件
if os.path.exists(r'e:/testpaper'):
pass
else:
os.makedirs(r'e:/testpaper')
#比对信息,把所需文件拷贝到e:/testpaper
lackFileInfo=[]
for t in demandInfo:
allfile=[]
for i in paperInfo:
q=i.split(',')
if re.search(t,q[0]):
allfile.append(i)
if allfile!=[]:
lastfile=random.choice(allfile)
q=lastfile.split(',')
if os.path.isfile(os.path.join(r'e:/testpaper',q[0].strip())):
pass
else:
shutil.copy(q[1].strip(),r'e:/testpaper')
okfile+=1
else:
lackFileInfo.append(t)
print '找到:', okfile,'份试卷'
#将缺少文件的详细信息存入文件lackoffile.txt
lackFile=open(r'e:/lackoffile.txt','w')
for lack in lackFileInfo:
for lacka in temdemandInfo1:
if lack==lacka.split('/t')[5]:
print >> lackFile,lacka
break
lackFile.close()
raw_input("press any key quit!")