23

CAD 図面 (「.dwg」、「.dxf」) を、サブフォルダーを含むソース ディレクトリから宛先ディレクトリにコピーし、元のディレクトリとサブフォルダーの構造を維持する方法を見つけようとしています。

  • 元のディレクトリ: H:\Tanzania...\Bagamoyo_Single_line.dw​​g
  • ソース ディレクトリ: H:\CAD\Tanzania...\Bagamoyo_Single_line.dw​​g

次の投稿内で@martineauから次の回答を見つけました: Python Factory Function

from fnmatch import fnmatch, filter
from os.path import isdir, join
from shutil import copytree

def include_patterns(*patterns):
    """Factory function that can be used with copytree() ignore parameter.

    Arguments define a sequence of glob-style patterns
    that are used to specify what files to NOT ignore.
    Creates and returns a function that determines this for each directory
    in the file hierarchy rooted at the source directory when used with
    shutil.copytree().
    """
    def _ignore_patterns(path, names):
        keep = set(name for pattern in patterns
                            for name in filter(names, pattern))
        ignore = set(name for name in names
                        if name not in keep and not isdir(join(path, name)))
        return ignore
    return _ignore_patterns

# sample usage

copytree(src_directory, dst_directory,
         ignore=include_patterns('*.dwg', '*.dxf'))

更新: 18:21。次のコードは期待どおりに機能しますが、include_patterns(' .dwg', ' .dxf')を含まないフォルダーを無視したい点が異なります。

4

1 に答える 1