python 複製與刪除

用python實現了一個小型的自動發版本的工具。這個“自動發版本”有點虛, 只是簡單地把debug 目錄下的配置文件複製到指定目錄,把Release下的生成文件複製到同一指定,過濾掉不需要的文件夾(.svn),然後再往這個指定目錄添加幾個特定的文件。

    這個是我的第一個python小程序。

    下面就來看其代碼的實現。

首先插入必要的庫:

1 import os 2 import os.path 3 import shutil 4 import time, datetime

然後就是一大堆功能函數。第一個就是把某一目錄下的所有文件複製到指定目錄中:

複製代碼
1 def removeFileInFirstDir(targetDir): 2 for file in os.listdir(targetDir): 3 targetFile = os.path.join(targetDir, file) 4 if os.path.isfile(targetFile): 5 os.remove(targetFile)

複製一級目錄下的所有文件到指定目錄:

複製代碼
1 def moveFileto(sourceDir, targetDir): 2 shutil.copy(sourceDir, targetDir)

往指定目錄寫文本文件:

1 def getCurTime(): 2 nowTime = time.localtime() 3 year = str(nowTime.tm_year) 4 month = str(nowTime.tm_mon) 5 if len(month) < 2: 6 month = '0' + month 7 day = str(nowTime.tm_yday) 8 if len(day) < 2: 9 day = '0' + day 10 return (year + '-' + month + '-' + day)
複製代碼

然後就是主函數的實現了:

複製代碼 1 if  __name__ =="__main__":
2     print "Start(S) or Quilt(Q) \n"
3     flag = True
4     while (flag):
5         answer = raw_input()
6         if  'Q' == answer:
7             flag = False
8         elif 'S'== answer :
9             formatTime = getCurTime()
10             targetFoldername = "Build " + formatTime + "-01"
11             Target_File_Path += targetFoldername
12
13             copyFiles(Debug_File_Path,   Target_File_Path)
14             removeFileInFirstDir(Target_File_Path)
15             coverFiles(Release_File_Path,  Target_File_Path)
16 moveFileto(Firebird_File_Path, Target_File_Path)
17             moveFileto(AssistantGui_File_Path,  Target_File_Path)
18             writeVersionInfo(Target_File_Path+"\\ReadMe.txt")
19             print "all sucess"
20         else:
21             print "not the correct command"
複製代碼

    感覺是果然簡單, 不過簡單的原因是因爲庫函數豐富,語言基本特性的簡單真沒感覺出來。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章