python 复制文件

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
source_root = 'D:\source'
test_root = 'D:\target'
files_list = 'files.txt'


def main():
    with open(files_list,'r') as f:
        for l in f:
            tmp = l.strip().replace('/commbook/trunk/commbook','')
            tmp = tmp.replace('/','\\')
            s = source_root + tmp
            t = test_root + tmp
            mkdir(t)
            print 'copy "'+ s +'" "'+ t +'"'
            os.system('copy "'+ s +'" "'+ t +'"')

    return 0

def mkdir(filename):
    segs = filename.split('\\')
    count = len(segs)
    for i in range(2,count):
        p = '\\'.join(segs[:i])
        if(not os.path.exists(p)):
            os.system('mkdir "'+ p +'"')


if __name__ == '__main__':
    main()

  

posted @ 2013-05-24 15:56  高天蒲  阅读(242)  评论(0编辑  收藏  举报
[url=http://t.sina.com.cn/1084588625?s=6uyXnP][img]http://service.t.sina.com.cn/widget/qmd/1084588625/a57ab42b/1.png[/img][/url]