Reputation: 10021
If I have a folder which has multiple subfolders, each has same structure sub1, sub2 and sub3:
├─a
│ ├─sub1
│ ├─sub2
│ └─sub3
├─b
│ ├─sub1
│ ├─sub2
│ └─sub3
└─c
├─sub1
├─sub2
└─sub3
...
I want to copy sub1
and sub2
if there are image files inside and ignore other subfolders (sub3...
) while keeping tree structure of directory. This is expected result:
├─a
│ ├─sub1
│ └─sub2
├─b
│ ├─sub1
│ └─sub2
└─c
├─sub1
└─sub2
...
What should I do to get expected result? I have try following code but it only copy sub1
, sub2
and sub3
, I got TypeError: copy() takes no arguments (1 given)
.
Thanks to @PrasPJ.
import os
import os.path
import shutil
import fnmatch
src_dir= ['C:/Users/User/Desktop/source/input'] # List of source dirs
included_subdirs = ['sub1', 'sub2'] # subdir to include from copy
dst_dir = 'C:/Users/User/Desktop/source/output' # folder for the destination of the copy
for root_path in src_dir:
#print(root_path)
for root, dirs, files in os.walk(root_path): # recurse walking
for dir in included_subdirs:
#print(dir)
if dir in dirs:
source = os.path.join(root,dir)
f_dst = source.replace(root_path, dst_dir)
print (source, f_dst)
for file in os.listdir(source):
print(file)
if file.endswith(".jpg") or file.endswith(".jpeg"):
shutil.copytree(source, f_dst)
Reference related:
Python shutil copytree: use ignore function to keep specific files types
How to copy contents of a subdirectory in python
Upvotes: 0
Views: 958
Reputation: 66
Here is the update
for root_path in list_of_dirs_to_copy:
for root, dirs, files in os.walk(root_path): # recurse walking
path_to_copy = {}
for dir in included_subdirs:
if dir in dirs:
source = os.path.join(root,dir)
for f in os.listdir(source):
if os.path.isfile(os.path.join(source,f)) and (f.endswith("jpg") or f.endswith("jpeg")):
f_dest = source.replace(root_path, dest_dir)
path_to_copy.update({source: f_dest})
if len(path_to_copy) == len(included_subdirs):
for source, f_dest in path_to_copy.iteritems():
shutil.copytree(source,f_dest)
Upvotes: 1
Reputation: 66
import os
import os.path
import shutil
import fnmatch
list_of_dirs_to_copy = ['C:/Users/User/Desktop/test/input'] # List of source dirs
included_subdirs = ['sub1', 'sub2'] # subdir to include from copy
dest_dir = 'C:/Users/User/Desktop/test/output' # folder for the destination of the copy
for root_path in list_of_dirs_to_copy:
print(root_path)
for root, dirs, files in os.walk(root_path): # recurse walking
for dir in included_subdirs:
print(dir)
if dir in dirs:
source = os.path.join(root,dir)
for f in os.listdir(source):
print os.path.join(source,f)
if os.path.isfile(os.path.join(source,f)) and (f.endswith("jpg") or f.endswith("jpeg")):
f_dest = source.replace(root_path, dest_dir)
print source, f_dest
shutil.copytree(source,f_dest)
break # found any one matching file
Upvotes: 1
Reputation: 66
Hi you are trying to use dictionary's copy function which is wrong
import os
import os.path
import shutil
import fnmatch
list_of_dirs_to_copy = ['C:/Users/User/Desktop/test/input'] # List of source dirs
included_subdirs = ['sub1', 'sub2'] # subdir to include from copy
dest_dir = 'C:/Users/User/Desktop/test/output' # folder for the destination of the copy
for root_path in list_of_dirs_to_copy:
print(root_path)
for root, dirs, files in os.walk(root_path): # recurse walking
for dir in included_subdirs:
print(dir)
if dir in dirs:
source = os.path.join(root,dir)
f_dest = source.replace(root_path, dest_dir)
print source, f_dest
shutil.copytree(source,f_dest)
Upvotes: 2