Julien
Julien

Reputation: 619

Copy directory in another directory

I have a directory D:/INPUT/test1 that I'd like to copy in another directory D:/OUTPUT.

I tried many methods but none of them have worked.

For example I tried the method explained at Copy directory contents into a directory with python, that is to say :

import distutils.core

# copy subdirectory example
fromDirectory = "D:/INPUT/test1"
toDirectory = "D:/OUTPUT"

distutils.dir_util.copy_tree(fromDirectory, toDirectory)

The directory D:/OUTPUT is well created but there is nothing inside.

Then I tried the shutil.copytree method but I get the same result.

Upvotes: 1

Views: 406

Answers (1)

Robert P
Robert P

Reputation: 157

import shutil, errno

def copyanything(src, dst):
try:
    shutil.copytree(src, dst)
except OSError as exc: # python >2.5
    if exc.errno == errno.ENOTDIR:
        shutil.copy(src, dst)
    else: raise

Upvotes: 3

Related Questions