5

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

2
  • You could use subprocess [1] to do a recursive copy but exactly what to call depends on OS. Commented Jul 5, 2022 at 7:53
  • 1
    This can help. Commented Jul 5, 2022 at 8:03

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.