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

Sruthi
  • 2,908
  • 1
  • 11
  • 25
preethy tulpi
  • 415
  • 1
  • 5
  • 15

1 Answers1

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.

AGN Gazer
  • 8,025
  • 2
  • 27
  • 45