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
3,0281 gold badge14 silver badges26 bronze badges
asked Feb 14, 2018 at 4:58
preethy tulpi
4151 gold badge7 silver badges15 bronze badges
1 Answer 1
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.
answered Feb 14, 2018 at 5:07
AGN Gazer
8,4272 gold badges31 silver badges49 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py