shutil.copytree without files

前端 未结 5 2096
刺人心
刺人心 2021-02-13 19:39

I\'m trying use shutil.copytree:

shutil.copytree(SOURCE_DIR, TARGET_DIR, ignore=None)

This copy also files in folder. I need copy only folders

5条回答
  •  感动是毒
    2021-02-13 20:00

    Here's an implementation of @Oz123's solution which is based on os.walk():

    import os
    
    def create_empty_dirtree(srcdir, dstdir, onerror=None):
        srcdir = os.path.abspath(srcdir)
        srcdir_prefix = len(srcdir) + len(os.path.sep)
        os.makedirs(dstdir)
        for root, dirs, files in os.walk(srcdir, onerror=onerror):
            for dirname in dirs:
                dirpath = os.path.join(dstdir, root[srcdir_prefix:], dirname)
                try:
                    os.mkdir(dirpath)
                except OSError as e:
                    if onerror is not None:
                        onerror(e)
    

提交回复
热议问题