python 檔案拷貝

來源:互聯網
上載者:User

標籤:檔案拷貝   rip   檔案   小程式   file   size   版本   tar   bar   

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

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

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

首先插入必要的庫:

 

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

 

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

 

1 def copyFiles(sourceDir, targetDir):
2 if sourceDir.find(".svn") >0:
3 return
4 for file in os.listdir(sourceDir):
5 sourceFile = os.path.join(sourceDir, file)
6 targetFile = os.path.join(targetDir, file)
7 if os.path.isfile(sourceFile):
8 if not os.path.exists(targetDir):
9 os.makedirs(targetDir)
10 if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))):
11 open(targetFile, "wb").write(open(sourceFile, "rb").read())
12 if os.path.isdir(sourceFile):
13 First_Directory = False
14 copyFiles(sourceFile, targetFile)

 

刪除一級目錄下的所有檔案:

 

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 coverFiles(sourceDir, targetDir):
2 for file in os.listdir(sourceDir):
3 sourceFile = os.path.join(sourceDir, file)
4 targetFile = os.path.join(targetDir, file)
5 #cover the files
6 if os.path.isfile(sourceFile):
7 open(targetFile, "wb").write(open(sourceFile, "rb").read())

 

複製指定檔案到目錄:

 

1 def moveFileto(sourceDir, targetDir):
2 shutil.copy(sourceDir, targetDir)

 

往指定目錄寫文字檔:

 

1 def writeVersionInfo(targetDir):
2 open(targetDir, "wb").write("Revison:")

 

返回當前的日期,以便在建立指定目錄的時候用:

 

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"

 

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

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.