python文件夹复制

来源:互联网 发布:springmvc 初始化数据 编辑:程序博客网 时间:2024/05/21 18:34

       目标:将一个文件夹下的所有文件(文件夹以及文件)复制到另一个文件夹目录下

       

def copyFiles(srcPath,targetPath):    '''            将src文件夹下的所有文件,包括文件夹全部拷贝至targetpath目录下    '''    lst = os.listdir(srcPath)    for filename in lst:        tmpSrcpath = os.path.join(srcPath,filename)        tmptargetPath = os.path.join(targetPath,filename)        print filename        if os.path.isdir(tmpSrcpath):            if os.path.exists(tmptargetPath):                shutil.rmtree(tmptargetPath)              os.mkdir(tmptargetPath)              copyFiles(tmpSrcpath, tmptargetPath)        else:            if filename == unicode('desktop.ini','utf-8'):                continue                     shutil.copy(tmpSrcpath, tmptargetPath)                    if __name__ == '__main__':    copyFiles('E:/Python/'.decode('utf-8'), 'e:/test'.decode('utf-8'))
       需要注意的是desktop.ini这个文件,说实话,这文件哪里来的我也不知道

       整个过程就是一个递归,很简单,没什么难度,输入参数时一定要用utf-8 decode一下,不然中文目录会无法正常处理

0 0