Welcome, guest | Sign In | My Account | Store | Cart

take a glob expression, a source directory and a destination directory to copy each files matching the glob in the appropriate directory

glob = */*.txt
src_dir = ./a/b
dst_dir = /z/x/y

if the glob match a file ./a/b/c/foo.txt, it will copy it in /z/x/y/c/foo.txt (and create the missing directory if needed)

Require Python3.4, code tab indented

Python, 6 lines
1
2
3
4
5
6
def copy_file(glob, dst_dir, src_dir=Path('.')) :
	for src_pth in src_dir.glob(glob) :
		dst_pth = dst_dir / src_pth.relative_to(src_dir)
		if src_pth.is_file() and not dst_pth.parent.exists() :
			dst_pth.parent.mkdir(parents=True)
		shutil.copy(str(src_pth), str(dst_pth))
Created by yota on Fri, 6 Feb 2015 (GPL3)
Python recipes (4591)
yota's recipes (13)

Required Modules

  • (none specified)

Other Information and Tasks