python复制目录下的文件

#!/usr/bin/python
# -- coding: UTF-8 --
import os, shutil;

def getdirfiles(dire):
    dire = dire.rstrip('/');
    filelist = [];
    pathlist = os.walk(dire);
    for pair in pathlist:
        if pair[2]:
            for name in pair[2]:
                filelist.append(pair[0]+'/'+name);
    return filelist;


def copydirfiles(srcdir, dstdir):
    srcdir = srcdir.rstrip('/');
    dstdir = dstdir.rstrip('/');
    filelist = [];
    pathlist = os.walk(srcdir);
    for pair in pathlist:
        if pair[2]:
            for name in pair[2]:
                filelist.append(pair[0].replace(srcdir, '')+'/'+name);
    for f in filelist:
        srcfile = srcdir+f;
        dstfile = dstdir+f;
        dstsubdir = os.path.dirname(dstfile);
        if not os.path.isdir(dstsubdir):
            os.makedirs(dstsubdir);
        shutil.copy(srcfile, dstfile);


你可能感兴趣的:(python,复制目录)