Script 1
# Filename: *** Add a label before each question in parserv4.py <qstart> # Author: Liya # Email: bruceleeliya@163.com # 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) # Generate a random number 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> mark the start of a question 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: add notes by Lee at 2010.01.21 Qlist. append ( '' . Join (qtemp) + ' \ N ' ) Startnumber + = 1 Qlist = Randomlist (qlist) qlist. insert (0, '' . Join (titlelist) + ' \ 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 (): ''' MainProgramEntry Function ''' 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 EIn Error: Print ' Error file: "% s" \ n ' % E # --------------------------------------------------------------------------------# Shownumber = 10 If _ Name __ = ' _ Main __ ' : Main ()
Script 2
# EXE + *. TCE => *. Zip # Create by Liya at 2011.05.03 Import OS, zipfileexefile = 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
I would like to commemorate my one-week study of Python. But now, I don't remember anything ~~~