首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Python按名称复制特定子目录

使用Python按名称复制特定子目录
EN

Stack Overflow用户
提问于 2019-02-19 16:48:12
回答 3查看 459关注 0票数 0

如果我有一个包含多个子文件夹的文件夹,每个子文件夹都有相同的结构sub1、sub2和sub3:

代码语言:javascript
复制
├─a
│  ├─sub1
│  ├─sub2
│  └─sub3
├─b
│  ├─sub1
│  ├─sub2
│  └─sub3
└─c
    ├─sub1
    ├─sub2
    └─sub3
...

如果里面有图像文件,我想复制sub1sub2,并忽略其他子文件夹(sub3...),同时保持目录的树形结构。这是预期的结果:

代码语言:javascript
复制
├─a
│  ├─sub1
│  └─sub2
├─b
│  ├─sub1
│  └─sub2
└─c
    ├─sub1
    └─sub2
...

我应该怎么做才能得到预期的结果?我已经尝试了以下代码,但它只复制sub1sub2sub3,我得到了TypeError: copy() takes no arguments (1 given)

感谢@PrasPJ。

代码语言:javascript
复制
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)

与参考相关:

https://www.daniweb.com/programming/software-development/threads/356380/how-to-copy-folders-directories-using-python

Python shutil copytree: use ignore function to keep specific files types

How to copy contents of a subdirectory in python

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-02-19 17:45:19

您好,您正在尝试使用字典的复制功能,这是错误的

代码语言:javascript
复制
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) 
票数 2
EN

Stack Overflow用户

发布于 2019-02-19 20:52:47

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2019-02-21 20:17:49

这是最新的

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54762084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档