用Python處理的那些指令碼

來源:互聯網
上載者:User

指令碼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一周的日子。可現在,我什麼都不記得了~~~

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.