欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python備份目錄及目錄下的全部?jī)?nèi)容的實(shí)現(xiàn)方法

 更新時(shí)間:2016年06月12日 16:19:11   投稿:jingxian  
下面小編就為大家?guī)硪黄狿ython備份目錄及目錄下的全部?jī)?nèi)容的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

本來是想寫一個(gè)東西可以直接調(diào)用TortoiseSVN保存當(dāng)前代碼到一個(gè)分枝下的。

可惜調(diào)用SVN的部分還在研究。就先寫了目錄拷貝的部分。

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

這里先謝謝了。 :)

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

生成一個(gè)項(xiàng)目名稱加日期時(shí)間組成的文件夾名為分枝名稱。把當(dāng)前項(xiàng)目下的全部?jī)?nèi)容

拷貝到這個(gè)目錄下。

然后要做的研究就是調(diào)用TortoiseSVN命令嵌入這部分代碼。

現(xiàn)在看代碼:

1. 讀取配置文件

配置文件很簡(jiǎn)單。用的就是txt文件。 格式類似于:

# root:/Users/lichallenger/test_src/
# project:test
# destination:/Users/lichallenger/test_dst/

BTW: 我用的是Mac所以目錄格式是這樣的。如果你用的是Windows的話請(qǐng)適

當(dāng)修改配置文件。

讀文件就是最簡(jiǎn)單的了。直接用標(biāo)準(zhǔn)庫(kù)的文件操作模塊打開文件,讀出全部的配置。一共就三行,所以

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

# 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: 

 f.close() 

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

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

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

# copy dir(s) & file(s) to configured path
# author: bruce li

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):
    try:
      shutil.copytree(self.src_path,self.dest_path)
    except:
      raise  

  @staticmethod
  def  move_src_content(src, dest):
    try:
      shutil.copytree(src_path,dest_path)
    except:
      raise

3. 綜合調(diào)用

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

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

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

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

 # main of dir copy function

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()

有時(shí)間我會(huì)研究下TortoiseSVN調(diào)用那塊的東西。估計(jì)不會(huì)難,就是調(diào)用exe傳參的問題。

本人初學(xué)Python,如有問題敬請(qǐng)指正!謝謝。

以上這篇Python備份目錄及目錄下的全部?jī)?nèi)容的實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論