def cp_parents(files, target_dir: Union[str, Path]):
<EOL><INDENT>if isinstance(files, (str, Path)):<EOL><INDENT>files = [files]<EOL><DEDENT>files = (Path(f).expanduser() for f in files)<EOL>target_dir = Path(target_dir).expanduser()<EOL>for f in files:<EOL><INDENT>newpath = target_dir / f.parent<EOL>newpath.mkdir(parents=True, exist_ok=True)<EOL>shutil.copy2(f, newpath)<EOL><DEDENT><DEDENT>
This function requires Python >= 3.6.

This acts like bash cp --parents in Python
inspiration from
http://stackoverflow.com/questions/15329223/copy-a-file-into-a-directory-with-its-original-leading-directories-appended

example
source: /tmp/e/f
dest: /tmp/a/b/c/d/
result: /tmp/a/b/c/d/tmp/e/f

cp_parents('/tmp/a/b/c/d/boo','/tmp/e/f')
cp_parents('x/hi','/tmp/e/f/g')  --> copies ./x/hi to /tmp/e/f/g/x/hi