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 without ANY files. How to do it?
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 without ANY files. How to do it?
If you want ignore pattern functionality with
os.walk()
, then:This will ignore
.git
directory.Note: This requires
Python >=3.2
as I used theexist_ok
option withmakedirs
which isn't available on older versions.Here's an implementation of @Oz123's solution which is based on
os.walk()
:You should consider using
os.walk
.Here is an example for os.walk. This way you could list all the directories and then create them with
os.mkdir
.use
distutils.dir_util.create_tree
to just copy directory structure (not files)note : the argument
files
is a list of filenames. if you want something that would work as shutils.copytree:You can do that by providing a "ignore" function
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)