Python 复制目录结构和文件,不包括文件类型、子文件夹。。。是否可以在不编写整个脚本的情况下使用标准库?

Python 复制目录结构和文件,不包括文件类型、子文件夹。。。是否可以在不编写整个脚本的情况下使用标准库?,python,Python,我正在寻找一种快速复制整个目录结构(包括子文件夹和文件)的方法,条件如下: 复制目标中不存在的文件或源较新的文件 允许排除子文件夹列表,即[temp'、.git'] 允许按类型排除文件,例如['.txt'、'.pyc'、'*.zip'] 我已经用shutil.copy和copytree看到了一些答案,但没有一个答案能满足我的要求 我希望这可以通过使用一个标准的实用程序来实现,提供参数等。如果没有,我将编写一个脚本来实现 这就是我最后写的。。。它完成了任务,我希望这个基本功能将由一个标准库提供 i

我正在寻找一种快速复制整个目录结构(包括子文件夹和文件)的方法,条件如下:

  • 复制目标中不存在的文件或源较新的文件
  • 允许排除子文件夹列表,即[temp'、.git']
  • 允许按类型排除文件,例如['.txt'、'.pyc'、'*.zip']
  • 我已经用
    shutil.copy
    copytree
    看到了一些答案,但没有一个答案能满足我的要求

    我希望这可以通过使用一个标准的实用程序来实现,提供参数等。如果没有,我将编写一个脚本来实现

    这就是我最后写的。。。它完成了任务,我希望这个基本功能将由一个标准库提供

    import os, sys, pathlib, shutil
    
    def copy_files_on_tree(srcdir, dstdir, sub_folder_to_include, extensions_to_include):
        srcdir = str(pathlib.Path(srcdir)).replace('\\', '/')
        dstdir = str(pathlib.Path(dstdir)).replace('\\', '/')
        for dirpath, dirs, files in os.walk(pathlib.Path(srcdir)):
            this_dir = dirpath.replace('\\', "/")        
            if os.path.basename(this_dir) in sub_folder_to_include:
                dest_dir = this_dir.replace(srcdir, dstdir)
                # create folder in the destinatin if it does not exist
                pathlib.Path(dest_dir).mkdir(parents=True, exist_ok=True)                
                for filename in files:
                    dest_file = os.path.join(dest_dir, os.path.basename(filename))
                    source_file = os.path.join(this_dir, filename)
                    if os.path.isfile(source_file) and filename.endswith(extensions_to_include):
                        # copy file if destination is older by more than a second, or does not exist
                        if (not os.path.exists(dest_file)) or (os.stat(source_file).st_mtime - os.stat(dest_file).st_mtime > 1):
                            print (f'Copying {source_file} to {dest_dir}')
                            shutil.copy2(source_file, dest_dir)
                        else:
                            print (f'.....Skipping {source_file} to {dest_dir}')
    
    srcdir = 'c:/temp/a'
    dstdir = 'c:/temp/j'
    sub_folder_to_include = ('a', 'aa','bb')
    extensions_to_include = ('.py', '.png', '.gif', '.txt')
    
    copy_files_on_tree(srcdir, dstdir, sub_folder_to_include, extensions_to_include)
    
    
    这就是解决方案:

    import os, sys, pathlib, shutil
    
    def copy_files_on_tree(srcdir, dstdir, sub_folder_to_include, extensions_to_include):
        srcdir = str(pathlib.Path(srcdir)).replace('\\', '/')
        dstdir = str(pathlib.Path(dstdir)).replace('\\', '/')
        for dirpath, dirs, files in os.walk(pathlib.Path(srcdir)):
            this_dir = dirpath.replace('\\', "/")        
            if os.path.basename(this_dir) in sub_folder_to_include:
                dest_dir = this_dir.replace(srcdir, dstdir)
                # create folder in the destinatin if it does not exist
                pathlib.Path(dest_dir).mkdir(parents=True, exist_ok=True)                
                for filename in files:
                    dest_file = os.path.join(dest_dir, os.path.basename(filename))
                    source_file = os.path.join(this_dir, filename)
                    if os.path.isfile(source_file) and filename.endswith(extensions_to_include):
                        # copy file if destination is older by more than a second, or does not exist
                        if (not os.path.exists(dest_file)) or (os.stat(source_file).st_mtime - os.stat(dest_file).st_mtime > 1):
                            print (f'Copying {source_file} to {dest_dir}')
                            shutil.copy2(source_file, dest_dir)
                        else:
                            print (f'.....Skipping {source_file} to {dest_dir}')
    
    srcdir = 'c:/temp/a'
    dstdir = 'c:/temp/j'
    sub_folder_to_include = ('a', 'aa','bb')
    extensions_to_include = ('.py', '.png', '.gif', '.txt')
    
    copy_files_on_tree(srcdir, dstdir, sub_folder_to_include, extensions_to_include)
    

    我将感谢评论时,向下这篇文章。。。