加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 将特定文件复制到新文件夹,同时保留原始子目录树

发布时间:2020-12-20 11:49:16 所属栏目:Python 来源:网络整理
导读:我有一个包含许多子目录的大型目录,我正在尝试排序,我正在尝试将特定文件类型复制到新文件夹,但我想维护原始子目录. def copyFile(src,dest):try: shutil.copy(src,dest)except shutil.Error as e: print('Error: %s' % e)except IOError as e: print('Error
我有一个包含许多子目录的大型目录,我正在尝试排序,我正在尝试将特定文件类型复制到新文件夹,但我想维护原始子目录.

def copyFile(src,dest):
try:
    shutil.copy(src,dest)
except shutil.Error as e:
    print('Error: %s' % e)
except IOError as e:
    print('Error: %s' % s.strerror)


for root,directories,files in os.walk(directory):
    for directoryname in directories:
        dirpath = os.path.join(root,directoryname)
        dir_paths.append(dirpath)
        dir_names.append(directoryname)

        if not os.listdir(dirpath): #Cheching if directory is empty
            print("Empty")
            EmptyDirs.append(directoryname) #Add directory name to empty directory list
            EmptyDirPath.append(dirpath)
        else:
            pass

    for filename in files:
        filepath = os.path.join(root,filename)
        file_paths.append(filepath)
        file_names.append(filename)

    if filename.lower().endswith(".sldasm"):
            print(filename.encode('utf8'))
            SolidModels.append(filename)
            copyFile(filepath,dest)
    elif filename.lower().endswith(".sldprt"):
            print(filename.encode('utf8'))
            SolidModels.append(filename)
            copyFile(filepath,dest)
    else:
        pass

这是我现在使用的代码,但它只是复制文件而不复制它们最初所在的子目录,因此它们在新文件夹中完全没有组织.

这是使用copytree的新代码,但是现在特定文件不会复制,只有子目录才能复制.

def copytree(src,dst,symlinks=False,ignore=None):
    names = os.listdir(src)
    if ignore is not None:
        ignored_names = ignore(src,names)
    else:
        ignored_names = set()

os.makedirs(dst)
errors = []

for name in names:
    if name in ignored_names:
        continue

    srcname = os.path.join(src,name)
    dstname = os.path.join(dst,name)

    try:
        if symlinks and os.path.islink(srcname):
            linkto = os.readlink(srcname)
            os.symlink(linkto,dstname)
        elif os.path.isdir(srcname):
            copytree(srcname,dstname,symlinks,ignore)
        else:
            if src is "*.sldasm":
                copy2(srcname,dstname)
            elif src is "*.sldprt":
                copy2(srcname,dstname)

    except (IOError,os.error) as why:
            errors.append((srcname,str(why)))

解决方法

您可以使用其可选的ignore关键字参数,使用 shutil.copytree()函数执行所需操作.棘手的部分是,如果给定,它必须是一个可调用的,它返回每个目录中不应复制的内容,而不是应该复制的内容.

但是,可以编写类似于shutil.ignore_patterns()的工厂函数,该函数创建一个执行所需操作的函数,并将其用作ignore关键字参数的值.

首先返回的函数通过fnmatch.filter()函数确定要保留哪些文件,然后将它们从给定目录中的所有内容列表中删除,除非它们是子目录名称,在这种情况下它们会留待以后[递归]处理. (这就是它复制整个树的原因以及你尝试编写自己的copytree()函数可能出错的原因).

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Works in Python 2.7 & 3.x

import fnmatch
from os.path import isdir,join

def include_patterns(*patterns):
    """ Function that can be used as shutil.copytree() ignore parameter that
    determines which files *not* to ignore,the inverse of "normal" usage.

    This is a factory function that creates a function which can be used as a
    callable for copytree()'s ignore argument,*not* ignoring files that match
    any of the glob-style patterns provided.

    ?patterns’ are a sequence of pattern strings used to identify the files to
    include when copying the directory tree.

    Example usage:

        copytree(src_directory,dst_directory,ignore=include_patterns('*.sldasm','*.sldprt'))
    """
    def _ignore_patterns(path,all_names):
        # Determine names which match one or more patterns (that shouldn't be
        # ignored).
        keep = (name for pattern in patterns
                        for name in fnmatch.filter(all_names,pattern))
        # Ignore file names which *didn't* match any of the patterns given that
        # aren't directory names.
        dir_names = (name for name in all_names if isdir(join(path,name)))
        return set(all_names) - set(keep) - set(dir_names)

    return _ignore_patterns


if __name__ == '__main__':

    from shutil import copytree,rmtree
    import os

    src = r'C:volsFilesPythonLibStack Overflow'
    dst = r'C:volsTemptemptest'

    # Make sure the destination folder does not exist.
    if os.path.exists(dst) and os.path.isdir(dst):
        print('removing existing directory "{}"'.format(dst))
        rmtree(dst,ignore_errors=False)

    copytree(src,ignore=include_patterns('*.png','*.gif'))

    print('done')

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读