天天看點

Python備份目錄及目錄下的全部内容

python備份目錄及目錄下的全部内容

本來是想寫一個東西可以直接調用tortoisesvn儲存目前代碼到一個分枝下的。

可惜調用svn的部分還在研究。就先寫了目錄拷貝的部分。

如果有喜歡研究python的童鞋願意提供想法或者建議的話,

這裡先謝謝了。 :)

就目錄拷貝的部分,思想很簡單。讀配置檔案中的配置資訊。

生成一個項目名稱加日期時間組成的檔案夾名為分枝名稱。把目前項目下的全部内容

拷貝到這個目錄下。

然後要做的研究就是調用tortoisesvn指令嵌入這部分代碼。

現在看代碼:

1. 讀取配置檔案

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

# root:/users/lichallenger/test_src/

# project:test

# destination:/users/lichallenger/test_dst/

btw: 我用的是mac是以目錄格式是這樣的。如果你用的是windows的話請适

當修改配置檔案。 

讀檔案就是最簡單的了。直接用标準庫的檔案操作子產品打開檔案,讀出全部的配置。一共就三行,是以

也不用考慮效率什麼的了。 

Python備份目錄及目錄下的全部内容

# open config file and read config information

# author: bruce li

class confighandler(object):

    #

    def __init__(self,config_path):

        '''initializer'''

        self.config_path = config_path

    #read config infor

    def read_config(self):

        f = open(self.config_path)

        try:

            self.all_lines = f.readlines()

        except:

            raise    

        else:

Python備份目錄及目錄下的全部内容

            f.close() 

2. 拷貝目錄和目錄内容

拷貝目錄用了shutil子產品。裡面有個方法可以直接把目錄和目錄下的全部内容拷貝到制定的其他目錄。

這樣就省得搞目錄周遊之類的代碼了。  

Python備份目錄及目錄下的全部内容

# copy dir(s) & file(s) to configured path

import shutil

class copyhandler(object):

    def __init__(self,src_path,dest_path):

        self.src_path = src_path

        self.dest_path = dest_path

    def move_content(self):

            shutil.copytree(self.src_path,self.dest_path)

    @staticmethod

    def    move_src_content(src, dest):

            shutil.copytree(src_path,dest_path)

            raise

Python備份目錄及目錄下的全部内容

3. 綜合調用

這裡用了time子產品擷取目前時間,然後生成目标檔案夾名稱的一部分。 

外界給python傳的系統參數的第一個是檔案名。這個檔案就相當于c#項目裡的program檔案一樣,

裡面會包含一個main函數。雖然這個函數不一定要命名為main。 

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

 # main of dir copy function

Python備份目錄及目錄下的全部内容

import sys

import time

from code_bk_cpy import *

from code_bk_config import *

#print __name__

def main():

    config_path = sys.argv[1]

    # check if path of configuration path is empty

    if (not config_path):

        print 'configuration information is needed'

        return -1     

    config_handler = confighandler(config_path)

    config_handler.read_config()

    config_list = config_handler.all_lines

    if len(config_list) != 3:

        print 'configuration information is not correct'

        return -1

    # set source

    sep = ':'

    current_datetime = time.localtime(time.time())

    root_path = config_list[0].split(sep)[1]

    prj_name = config_list[1].split(sep)[1]

    dst_path = config_list[2].split(sep)[1]

    root_path = (root_path + prj_name).replace('\n','')

    prj_folder = prj_name + str(current_datetime.tm_year) + str(current_datetime.tm_mon) + \

str(current_datetime.tm_mday) + str(current_datetime.tm_hour) + \

str(current_datetime.tm_min) + str(current_datetime.tm_sec)

    dst_path = (dst_path + '/' + prj_folder + '/').replace('\n','')

    copy_handler = copyhandler(root_path,dst_path)

    copy_handler.move_content()

    print 'content moved'

# start main function

print __name__

if __name__ == "__main__":

    main()

Python備份目錄及目錄下的全部内容

有時間我會研究下tortoisesvn調用那塊的東西。估計不會難,就是調用exe傳參的問題。

本人初學python,如有問題敬請指正!謝謝。 

歡迎加群互相學習,共同進步。qq群:ios: 58099570 | android: 330987132 | go:217696290 | python:336880185 | 做人要厚道,轉載請注明出處!http://www.cnblogs.com/sunshine-anycall/archive/2011/04/05/2005971.html