Python实践:根据项目复制目标文件
wibrst
|
1#
wibrst 发表于 2006-12-13 10:08
Python实践:根据项目复制目标文件工作中有若干项目,其中的目录结构都很相似,这个程序通过选择编号复制生成文件到其相应的便捷位置。 getInputedPrg函数中那个长行正好用来测试cublog能否装下。这个在blogspot不好调。 [color="#0000ff"][color="#0000ff"] import [color="#800000"]os [color="#0000ff"]import [color="#800000"]re [color="#0000ff"]import [color="#800000"]time [color="#000000"]aPrg[color="#00c600"]=[color="#000000"][[color="#ff00ff"]'ml1bw'[color="#000000"],[color="#ff00ff"]'cnc'[color="#000000"],[color="#ff00ff"]'enw'[color="#000000"]] [color="#0000ff"]def [color="#000000"]quotePath (sOrig): [color="#0000ff"]return [color="#ff00ff"]'"'[color="#00c600"]+[color="#000000"]sOrig[color="#00c600"]+[color="#ff00ff"]'"' [color="#0000ff"]def [color="#000000"]getInputedPrg(): [color="#0000ff"]while [color="#00c600"]True[color="#000000"]: [color="#0000ff"]try[color="#000000"]: [color="#000000"]sPrg[color="#00c600"]=[color="#808000"]raw_input[color="#000000"]([color="#ff00ff"]'Select Project by num,with\n '[color="#00c600"]+ [color="#ff00ff"]' '[color="#00c600"].[color="#808000"]join[color="#000000"]([[color="#ff00ff"]'%d.'[color="#00c600"]%[color="#000000"](i[color="#00c600"]+[color="#000000"]1)[color="#00c600"]+[color="#000000"]aPrg [color="#0000ff"]for [color="#000000"]i [color="#0000ff"]in [color="#808000"]range[color="#000000"]([color="#808000"]len[color="#000000"](aPrg))])[color="#00c600"]+[color="#ff00ff"]' :'[color="#000000"]) [color="#0000ff"]except [color="#ff0000"]EOFError[color="#000000"]: [color="#0000ff"]print [color="#ff00ff"]'Your Input is EOF,try again.("exit" to exit!)' [color="#0000ff"]continue [color="#0000ff"]if [color="#800000"]re[color="#00c600"].[color="#808000"]search[color="#000000"]([color="#ff00ff"]'\D*[1-3]\D*'[color="#000000"],sPrg): [color="#0000ff"]return [color="#000000"]sPrg [color="#0000ff"]elif [color="#800000"]re[color="#00c600"].[color="#808000"]search[color="#000000"]([color="#ff00ff"]'exit'[color="#000000"],sPrg,[color="#800000"]re[color="#00c600"].[color="#000000"]I): [color="#808000"]exit[color="#000000"]() [color="#0000ff"]else[color="#000000"]: [color="#0000ff"]print [color="#ff00ff"]'Your Input is not valid,try again.("exit" to exit!)' [color="#0000ff"]continue [color="#0000ff"]def [color="#808000"]main[color="#000000"](): [color="#000000"]sPrgPath[color="#00c600"]=[color="#000000"]aPrg[[color="#808000"]int[color="#000000"](getInputedPrg())[color="#00c600"]-[color="#000000"]1] [color="#000000"]sSrcPath[color="#00c600"]=[color="#ff00ff"]'D:\Dev\\'[color="#00c600"]+[color="#000000"]sPrgPath[color="#00c600"]+[color="#ff00ff"]'\prg\Workspace_project.xml\Pack ROM\ROMCS1.lbk' [color="#000000"]sTgtPath[color="#00c600"]=[color="#ff00ff"]'D:\Dev\\'[color="#00c600"]+[color="#000000"]sPrgPath[color="#00c600"]+[color="#ff00ff"]'\lbk\\' [color="#000000"]sTgtFile[color="#00c600"]=[color="#ff00ff"]"ROMCS1_wjw_"[color="#00c600"]+[color="#800000"]time[color="#00c600"].[color="#808000"]strftime[color="#000000"]([color="#ff00ff"]'%Y-%m-%d^%H-%M'[color="#000000"])[color="#00c600"]+[color="#ff00ff"]".lbk" [color="#000000"]sCmdCopy [color="#00c600"]=[color="#ff00ff"]' '[color="#00c600"].[color="#808000"]join[color="#000000"]([[color="#ff00ff"]'copy'[color="#000000"],quotePath(sSrcPath),quotePath(sTgtPath[color="#00c600"]+[color="#000000"]sTgtFile)]) [color="#0000ff"]print [color="#000000"]sCmdCopy [color="#0000ff"]if [color="#800000"]os[color="#00c600"].[color="#808000"]system[color="#000000"](sCmdCopy)[color="#00c600"]==[color="#000000"]0: [color="#008080"]# 0 means [color="#000000"]sCmdOpen[color="#00c600"]=[color="#ff00ff"]"start "[color="#00c600"]+[color="#000000"]sTgtPath [color="#0000ff"]print [color="#000000"]sCmdOpen [color="#800000"]os[color="#00c600"].[color="#808000"]system[color="#000000"](sCmdOpen [color="#000000"]) [color="#800000"]os[color="#00c600"].[color="#808000"]system[color="#000000"]([color="#ff00ff"]"exit"[color="#000000"]) [color="#0000ff"]print [color="#ff00ff"]"Copy successful!" [color="#0000ff"]pass [color="#0000ff"]else[color="#000000"]: [color="#0000ff"]print [color="#ff00ff"]"Failed!Check for your command!" [color="#808000"]main[color="#000000"]() sCmdOpen几句是为了打开windows窗口。在执行命令行时发现: start "D:\Dev\ml1bw\lbk\" 打开命令行窗口 start 'D:\Dev\ml1bw\lbk\' 不被识别 start D:\Dev\ml1bw\lbk\ 打开浏览器窗口,但是用os.system调用将会导致命令行窗口当机!---后来发现原来是系统正在打开Windows窗口,晕了,开了个贼占资源的东西 ... |