日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python如何備份目錄及目錄下的全部內(nèi)容

就目錄拷貝的部分,思想很簡單。讀配置文件中的配置信息。

創(chuàng)新互聯(lián)主營和田縣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP開發(fā),和田縣h5小程序設(shè)計(jì)搭建,和田縣網(wǎng)站營銷推廣歡迎和田縣等地區(qū)企業(yè)咨詢

生成一個(gè)項(xiàng)目名稱加日期時(shí)間組成的文件夾名為分枝名稱。把當(dāng)前項(xiàng)目下的全部內(nèi)容拷貝到這個(gè)目錄下。然后要做的研究就是調(diào)用TortoiseSVN命令嵌入這部分代碼。

現(xiàn)在看代碼:

1. 讀取配置文件

配置文件很簡單。用的就是txt文件。 格式類似于:

 
 
 
  1. # root:/Users/lichallenger/test_src/
  2. # project:test
  3. # destination:/Users/lichallenger/test_dst/

BTW: 我用的是Mac所以目錄格式是這樣的。如果你用的是Windows的話請適當(dāng)修改配置文件。

讀文件就是最簡單的了。直接用標(biāo)準(zhǔn)庫的文件操作模塊打開文件,讀出全部的配置。一共就三行,所以也不用考慮效率什么的了。

 
 
 
  1. # open config file and read config information
  2. # author: bruce li
  3. class ConfigHandler(object):
  4.     #
  5.     def __init__(self,config_path):
  6.         '''''initializer'''
  7.         self.config_path = config_path
  8.     
  9.     #read config infor
  10.     def read_config(self):
  11.         f = open(self.config_path)
  12.         try:
  13.             self.all_lines = f.readlines()
  14.         except:
  15.             raise    
  16.         else:
  17.             f.close() 

2. 拷貝目錄和目錄內(nèi)容

拷貝目錄用了shutil模塊。里面有個(gè)方法可以直接把目錄和目錄下的全部內(nèi)容拷貝到制定的其他目錄。

這樣就省得搞目錄遍歷之類的代碼了。

 
 
 
  1. # copy dir(s) & file(s) to configured path
  2. # author: bruce li
  3. import shutil
  4. class CopyHandler(object):
  5.     #
  6.     def __init__(self,src_path,dest_path):
  7.         self.src_path = src_path
  8.         self.dest_path = dest_path
  9.     def move_content(self):
  10.         try:
  11.             shutil.copytree(self.src_path,self.dest_path)
  12.         except:
  13.             raise    
  14.     @staticmethod
  15.     def    move_src_content(src, dest):
  16.         try:
  17.             shutil.copytree(src_path,dest_path)
  18.         except:
  19.             raise

3. 綜合調(diào)用

這里用了time模塊獲取當(dāng)前時(shí)間,然后生成目標(biāo)文件夾名稱的一部分。

外界給python傳的系統(tǒng)參數(shù)的***個(gè)是文件名。這個(gè)文件就相當(dāng)于C#項(xiàng)目里的Program文件一樣,

里面會包含一個(gè)main函數(shù)。雖然這個(gè)函數(shù)不一定要命名為main。

 
 
 
  1.  # main of dir copy function
  2. import sys
  3. import time
  4. from code_bk_cpy import *
  5. from code_bk_config import *
  6. #print __name__
  7. def main():
  8.     config_path = sys.argv[1]
  9.        # check if path of configuration path is empty
  10.     if (not config_path):
  11.         print 'configuration information is needed'
  12.         return -1     
  13.     config_handler = ConfigHandler(config_path)
  14.     config_handler.read_config()
  15.     config_list = config_handler.all_lines
  16.     if len(config_list) != 3:
  17.         print 'configuration information is not correct'
  18.         return -1
  19.         # set source
  20.     sep = ':'
  21.     current_datetime = time.localtime(time.time())
  22.     root_path = config_list[0].split(sep)[1]
  23.     prj_name = config_list[1].split(sep)[1]
  24.     dst_path = config_list[2].split(sep)[1]
  25.     root_path = (root_path + prj_name).replace('\n','')
  26.     prj_folder = prj_name + str(current_datetime.tm_year) + str(current_datetime.tm_mon) + \
  27. str(current_datetime.tm_mday) + str(current_datetime.tm_hour) + \
  28. str(current_datetime.tm_min) + str(current_datetime.tm_sec)
  29.     dst_path = (dst_path + '/' + prj_folder + '/').replace('\n','')
  30.     copy_handler = CopyHandler(root_path,dst_path)
  31.     copy_handler.move_content()
  32.     print 'content moved'
  33. # start main function
  34. print __name__
  35. if __name__ == "__main__":
  36.     main()

還有注意下,Python代碼的換行符為\。


網(wǎng)站欄目:Python如何備份目錄及目錄下的全部內(nèi)容
文章來源:http://m.5511xx.com/article/djoodsj.html