python 根据配置文件复制文件

上班的时侯因为客户老是修改mojo的模板文件,搞的经常要更新很多的模板文件到服务器端。所以用python写了一个简单的脚本。

1. 配置文件 (mojo.ini)

[main]
skin_path=E:\Accentrix.src\Sunrider\Src\Web\Data\skins
mojo_templates=SunriderReplicator_skin_1,SunriderReplicator_skin_2,SunriderReplicator_skin_3,SunriderReplicator_skin_4
file_path=layout.master,images\01.png

[target]
path=z:\Data\skins

--在这里解释一下:

[main] 源路径的配置

skin_path:这个是mojo皮肤的目录位置

mojo_templates:模板目录用逗号分开表示多个,(其实也可以用其它的目录)

file_path:模板下面对应的文件,用逗号分开,也可以包含子目录(eg: images\image\k.jpg)

[target] 要复制到的位置

path:就是mojo皮肤所在的位置,参考main节点下面的 skin_path就可以了

--以下就是py的代码了

import os;
import ConfigParser;
import shutil;
def mojoUpdate():
    print "application will start to update"
    path=".\mojo.ini"
    main="main"
    config=ConfigParser.ConfigParser();
    config.read(path)
    skinpath=config.get(main,"skin_path")
   
    for _templateDirectory in config.get(main,"mojo_templates").split(","):
        SourceFolderpath=skinpath+"\\"+_templateDirectory
        for _file in config.get(main,"file_path").split(","):
            dest_path=config.get("target","path")+"\\"+_templateDirectory
            dest=dest_path+"\\"+_file
            fullpath=SourceFolderpath+ "\\" +_file
            print fullpath
            if os.path.exists(fullpath):
                print "copy file "+fullpath+" to " + dest
                CreateFolder(os.path.split(dest)[0])
                shutil.copyfile(fullpath,dest)
            else:
                print fullpath +" is not exists"
    print "copy completed"
#-----------------------------------------------------

#Recursion create folder
def CreateFolder(path):
    if os.path.exists(path)==False:       
        CreateFolder(os.path.split(path)[0])
        os.mkdir(path)

-------------------------

运行mojoUpdate()这个方法即可

posted on 2011-03-22 14:32  lock  阅读(349)  评论(0编辑  收藏  举报