自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python如何備份目錄及目錄下的全部內(nèi)容

開發(fā) 后端 前端
本來是想寫一個東西可以直接調(diào)用TortoiseSVN保存當前代碼到一個分枝下的??上д{(diào)用SVN的部分還在研究。就先寫了目錄拷貝的部分。

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

生成一個項目名稱加日期時間組成的文件夾名為分枝名稱。把當前項目下的全部內(nèi)容拷貝到這個目錄下。然后要做的研究就是調(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的話請適當修改配置文件。

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

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

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

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

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

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

3. 綜合調(diào)用

這里用了time模塊獲取當前時間,然后生成目標文件夾名稱的一部分。

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

里面會包含一個main函數(shù)。雖然這個函數(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代碼的換行符為\。

原文鏈接:http://www.cnblogs.com/sunshine-anycall/archive/2011/04/05/2005971.html

【編輯推薦】

  1. Python編輯利器:PyCharm初探
  2. 你不知道的 Python裝飾器的一個妙用
  3. 淺析Python中的列表解析和生成表達式
  4. 自制Python函數(shù)幫助查詢小工具
  5. 一個Python程序員的進化
責任編輯:陳貽新 來源: Coder Lee博客
相關(guān)推薦

2010-03-24 08:55:02

Python編程語言

2020-05-28 08:29:54

目錄腳本測試

2020-07-16 15:10:49

Pythonzip()可迭代對象

2009-11-30 17:00:05

VS2003窗口

2012-05-07 13:13:03

Python

2023-12-30 08:34:32

開發(fā)apkUri

2012-12-28 14:28:26

Android開發(fā)

2010-03-12 18:42:58

Python目錄

2011-09-07 09:44:32

SQL Server還原

2009-09-28 10:28:04

Linux刪除目錄子目錄

2010-03-23 15:52:43

Python操作文件

2009-12-08 12:06:03

linux當前目錄文件數(shù)

2012-02-20 23:16:42

Linux

2017-06-09 13:51:42

Linux命令刪除文件

2020-07-13 08:12:32

Linux命令瀏覽器

2020-07-13 23:42:27

Linux目錄命令

2011-02-25 11:06:45

Proftpf

2011-03-25 11:28:34

Cactirra目錄

2023-07-25 19:36:54

2019-04-17 08:00:00

Linuxrestic目錄
點贊
收藏

51CTO技術(shù)棧公眾號