shutil.copytree without files

前端 未结 5 2109
刺人心
刺人心 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 19:58

    You can do that by providing a "ignore" function

    def ig_f(dir, files):
        return [f for f in files if os.path.isfile(os.path.join(dir, f))]
    
    shutil.copytree(SRC, DES, ignore=ig_f)
    

    Basically, when you call copytree, it will recursively go to each child folder and provide a list of files in that folder to the ignore function to check if those files are suitable based on a pattern. The ignored files will be returned as a list in the end of the function and then, copytree will only copy items excluding from that list (which in your case, contains all the files in the current folder)

提交回复
热议问题