用Python处理的那些脚本

脚本1

# FileName: ****ParserV4.py 每道题目之前加上标签<QStart>
# Author: liya
# Email: [email protected]
# Version: 4.0
# Last Edit Time: 2010-04-15

import 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 = 10
if __name__ == '__main__':    
    main()

脚本2

# exe + *.tce => *.zip
# create by liya at 2011.05.03

import os,zipfile

exeFile = r'/home/****file/formal/****.exe' # EDIT
shortExeFile = '****.exe' # EDIT
tceDir = r'/home/****file/formal/****' # EDIT
destDir = r'/home/****file/formal/****/exe' # EDIT

if not os.path.exists(destDir):
    os.mkdir(destDir)

filelist = os.listdir(tceDir) # Get File List
for 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一周的日子。可现在,我什么都不记得了~~~

你可能感兴趣的:(用Python处理的那些脚本)