Why would shutil.copy() raise a permission exception when cp doesn't?
The operation that is failing is chmod
, not the copy itself:
File "/usr/lib/python2.7/shutil.py", line 91, in copymode
os.chmod(dst, mode)
OSError: [Errno 1] Operation not permitted: 'bin/styles/blacktie/images/ajax-loader-000000-e3e3e3.gif'
This indicates that the file already exists and is owned by another user.
shutil.copy
is specified to copy permission bits. If you only want the file contents to be copied, use shutil.copyfile(src, dst)
, or shutil.copyfile(src, os.path.join(dst, os.path.basename(src)))
if dst
is a directory.
A function that works with dst
either a file or a directory and does not copy permission bits:
def copy(src, dst):
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))
shutil.copyfile(src, dst)