0

I have below code:

import shutil
import os
def copy_files(file_path, symlinks=False, ignore=None):
    try:
        if os.path.isdir(src):
            shutil.copytree(src, dest, symlinks, ignore)
        else:
            shutil.copy2(src, dest)
    except IOError:
        pass

Receiving below error when executing code:

 shutil.copytree(src, dest, symlinks, ignore)
 File "/usr/lib64/python2.7/shutil.py", line 177, in copytree
 os.makedirs(dst)
 File "/usr/lib64/python2.7/os.py", line 157, in makedirs
 mkdir(name, mode) 
 OSError: [Errno 17] File exists: '


File path: /etc/ /var/tmp/
it works cp -r /etc/ /var/tmp/

Python2.7 I am using

1 Answer 1

2

Probably you get this error because the destination directory already exists. From the documentation of copytree():

The destination directory, named by dst, must not already exist;...

Try calling shutil.rmtree(dest, True) before shutil.copytree().

cp does not fail if destination exists: it just overwrites it.

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.