最近托管各个学院网站的服务器准备迁移,配置导出很蛋疼,加上那个防篡改的软件,有些东西只能手动复制,于是只好写了个脚本根据防篡改软件导出的配置来自动复制文件,代码如下,当然,给别人看没有缩进的python代码就是耍流氓的行为,这有gist

import os
from copy import deepcopy
import xml.etree.ElementTree as ET
import shutil

err_file = open("error.txt", "w")
success_file = open("success.txt", "w")

src_path_list = []

suffix_list = []

enter = raw_input("Press Enter key to continue to glean the files")
root = ET.parse("writeguard.xml").getroot()

for child in root:
    if child.tag == 'Exception':
        print 'Find Exception Dir'
        exception_xml = deepcopy(child)
        for path in exception_xml:
            print 'Add dir:', path.text
            src_path_list.append(path.text)

    if child.tag == 'SuffixExc':
        suffix_xml = deepcopy(child)
        suffix_list = suffix_xml.text.split('/')
        for i in range(0, len(suffix_list)):
            suffix_list[i] = suffix_list[i][1:]


for src_path in src_path_list:
    for dirpath, dirnames, filenames in os.walk(src_path):

        src_dir = dirpath
        dst_dir = os.path.abspath('.') + dirpath.split(':')[1]

        if not os.path.exists(dst_dir):
            os.makedirs(dst_dir)
    
        for filename in filenames:
            if os.path.splitext(filename)[1] in suffix_list:
                src_filename = src_dir + '\\' + filename
                dst_filename = dst_dir + '\\' + filename
                try:
                    shutil.copy(src_filename, dst_filename)
                    print src_filename, '====>', dst_filename
                    success_file.write(src_filename+'===>'+dst_filename+'\n')
                except Exception, e:
                    print src_filename, '====>', dst_filename
                    err_file.write(src_filename+'===>'+dst_filename+'\n')
                    print e

success_file.close()
err_file.close()

Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

required