复制目录结构和文件,不包括文件类型,子文件夹...可以使用标准库而不编写整个脚本吗?

问题描述 投票:-3回答:1

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

  1. 如果目标或源中不存在,则复制文件
  2. 允许排除子文件夹列表,即['temp','.git']
  3. 允许按类型排除文件,例如['。txt','。pyc','* .zip']

我已经看到一些使用shutil.copycopytree的答案,但没有一个人在做我想要的...

我希望可以通过使用标准实用程序之一提供参数等来完成此操作。否则,我将编写脚本来执行此操作...

这是我最终写的...它确实完成了工作,我希望这个基本功能可以由标准库之一提供...

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)

python
1个回答
0
投票
这是解决方案:

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)

© www.soinside.com 2019 - 2024. All rights reserved.