使用Python按名称复制特定子目录
如果我有一个包含多个子文件夹的文件夹,则每个文件夹都具有相同的结构sub1、sub2和sub3:使用Python按名称复制特定子目录,python,operating-system,shutil,copytree,Python,Operating System,Shutil,Copytree,如果我有一个包含多个子文件夹的文件夹,则每个文件夹都具有相同的结构sub1、sub2和sub3: ├─a │ ├─sub1 │ ├─sub2 │ └─sub3 ├─b │ ├─sub1 │ ├─sub2 │ └─sub3 └─c ├─sub1 ├─sub2 └─sub3 ... 如果里面有图像文件,我想复制sub1和sub2,并在保留目录树结构的同时忽略其他子文件夹(sub3…)。这是预期的结果: ├─a │ ├─sub1 │ └─sub2 ├─b │
├─a
│ ├─sub1
│ ├─sub2
│ └─sub3
├─b
│ ├─sub1
│ ├─sub2
│ └─sub3
└─c
├─sub1
├─sub2
└─sub3
...
如果里面有图像文件,我想复制sub1
和sub2
,并在保留目录树结构的同时忽略其他子文件夹(sub3…
)。这是预期的结果:
├─a
│ ├─sub1
│ └─sub2
├─b
│ ├─sub1
│ └─sub2
└─c
├─sub1
└─sub2
...
我应该怎么做才能得到预期的结果?我尝试了以下代码,但它只复制sub1
、sub2
和sub3
,我得到了TypeError:copy()不带参数(给定1)
感谢@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)
参考相关:
您好,您试图使用字典的复制功能,这是错误的
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)
您好,您试图使用字典的复制功能,这是错误的
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)
这是最新消息
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)
这是最新消息
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)
谢谢。还有一个问题,如果我想再添加一个条件,通过检查sub1和sub2中是否有以“jpg”或“jpeg”结尾的文件来决定是否复制它们?我该怎么办?你只有sub1和sub2中的文件吗?非常感谢。还有一个问题,如果我想再添加一个条件,通过检查sub1和sub2中是否有以“jpg”或“jpeg”结尾的文件来决定是否复制它们?我该怎么办?你只有sub1和sub2中的文件吗?谢谢,很抱歉,我需要再问一个问题:如果我复制子文件夹a、b和c。。。只有在sub1和sub2中都有图像文件的情况下?谢谢,对不起,我需要再问一个问题:如果我复制子文件夹a、b和c。。。仅当sub1和sub2中都有图像文件时?