4

I am aware that using
shutil.copy(src,dst) copies files and
shutil.copytree(src,dst) is used for copying directories.

Is there any way so I don't have to differentiate between copying folders and copying files?

Tysm

Luca.D
  • 57
  • 5
  • You could use `subprocess` \[[1](https://stackoverflow.com/a/89243/15399131)\] to do a recursive copy but exactly what to call depends on OS. – VRehnberg Jul 05 '22 at 07:53
  • 1
    [This](https://stackoverflow.com/questions/1994488/copy-file-or-directories-recursively-in-python) can help. – Jonathan Ciapetti Jul 05 '22 at 08:03

1 Answers1

4

You might want to take a look to this topic, where the same question was answered.

https://stackoverflow.com/a/1994840/17595642

Functions can be written to do so.

Here is the one implemented in the other topic :

import shutil, errno

def copyanything(src, dst):
    try:
        shutil.copytree(src, dst)
    except OSError as exc: # python >2.5
        if exc.errno in (errno.ENOTDIR, errno.EINVAL):
            shutil.copy(src, dst)
        else: raise
Art
  • 132
  • 6