指令碼1
# FileName: ****ParserV4.py 每道題目之前加上標籤<QStart># Author: liya# Email: bruceleeliya@163.com# Version: 4.0# Last Edit Time: 2010-04-15import sys, os, re, random#--------------------<F> randomList </F>--------------------#def randomList(qlist): rlist = [] rlistNumber = [] if len(qlist) > int(showNumber): rNumber = int(showNumber) else: rNumber = len(qlist) while True: currentNumber = random.randrange(1,rNumber+1) #產生一個隨機數 addNew = True for number in rlistNumber: if number == currentNumber: addNew = False break if addNew: rlistNumber.append(currentNumber) if len(rlistNumber) == rNumber: break for number in rlistNumber: rlist.append('<QStart>\n'+ qlist[number-1]) #<QStart> 標識一個考題的開始 return rlist#--------------------<F> parseLineList </F>--------------------#def parseLineList(lineList): titleList = [] qlist = [] qtemp = [] startNumber = 1 for line in lineList: if line.startswith(str(startNumber) + '.'): if line.startswith('1.'): titleList = lineList[:lineList.index(line)] qtemp = [] for l in lineList[lineList.index(line)+1:]: if not l.startswith('Answer'): qtemp.append(l) else: qtemp.append(l) break qtemp.insert(0,line.replace(str(startNumber) + '.', '').strip() + '\n') # TODO: 添加一個方法能隨機排序ABCD等選項的順序 Add Notes By Lee At 2010.01.21 qlist.append(''.join(qtemp) + '\n') startNumber += 1 qlist = randomList(qlist) qlist.insert(0,''.join(titleList) + '\n\n') return qlist#--------------------<F> parseFile </F>--------------------#def parseFile(filename, sourceDir, targetDir): fread = file(sourceDir + os.sep + filename) lineList = map(lambda line:line.strip() + '\n', fread.readlines()) lineList = filter(lambda line: not line.startswith('Version :') and not line.startswith('??') and not line == '\n', lineList) fread.close() targetFilename = targetDir + os.sep + filename fwrite = file(targetFilename, 'w') lineList = parseLineList(lineList) #Call Function parseLineList(lineList) fwrite.writelines(lineList) fwrite.close() return targetFilename#--------------------<F> main </F>--------------------#def main(): '''main 程式入口函數''' global showNumber sourceDir = raw_input('Enter Source Directory: -->') targetDir = raw_input('Enter Target Directory: -->') showNumber = raw_input('Enter Show Number: -->') #sourceDir = r'E:\MyProject\Project\Tool Software\Python Project\txt' #Test #targetDir = r'E:\MyProject\Project\Tool Software\Python Project\txt_temp' #Test #showNumber = 10 #Test print 'Source Directory: "%s"\nTarget Directory: "%s"\n' % (sourceDir, targetDir) if not os.path.exists(targetDir): print '"%s" is not exists.' % targetDir os.mkdir(targetDir) print 'Create "%s" Success.\n' % targetDir filelist = os.listdir(sourceDir) #Get File List error = [] i = 0; while(i < 8): i = i + 1; temptargetDir = targetDir + os.sep + 'demo_txt' + str(i) if not os.path.exists(temptargetDir): print '"%s" is not exists.' % temptargetDir os.mkdir(temptargetDir) print 'Create "%s" Success.\n' % temptargetDir for filename in filelist: print 'Parser "%s" ...' % (sourceDir + os.sep + filename) try: tf = parseFile(filename, sourceDir, temptargetDir) print 'Parser Done: "%s"\n' % tf except: error.append(tf) if len(error) == 0: print 'All File Parser Success!' else: print 'Error Number: %d\n' % len(error) for e in error: print 'Error File: "%s"\n' % e#--------------------------------------------------------------------------------# showNumber = 10if __name__ == '__main__': main()
指令碼2
# exe + *.tce => *.zip# create by liya at 2011.05.03import os,zipfileexeFile = r'/home/****file/formal/****.exe' # EDITshortExeFile = '****.exe' # EDITtceDir = r'/home/****file/formal/****' # EDITdestDir = r'/home/****file/formal/****/exe' # EDITif not os.path.exists(destDir): os.mkdir(destDir)filelist = os.listdir(tceDir) # Get File Listfor tceFile in filelist: if os.path.isfile(tceDir + os.sep + tceFile): try: print tceFile destFile = destDir + os.sep + tceFile[:tceFile.index('.V')] + '.zip' f = zipfile.ZipFile(destFile,'w',zipfile.ZIP_DEFLATED) f.write(exeFile,shortExeFile) f.write(tceDir + os.sep + tceFile,tceFile) f.close() print destFile except: print 'error ' + tceFile
謹以此紀念我學習Python一周的日子。可現在,我什麼都不記得了~~~