shutil.copy() is raising a permissions error:
Traceback (most recent call last):
File "copy-test.py", line 3, in <module>
shutil.copy('src/images/ajax-loader-000000-e3e3e3.gif', 'bin/styles/blacktie/images')
File "/usr/lib/python2.7/shutil.py", line 118, in copy
copymode(src, dst)
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'
copy-test.py:
import shutil
shutil.copy('src/images/ajax-loader-000000-e3e3e3.gif', 'bin/styles/blacktie/images')
I am running copy-test.py from the command line:
python copy-test.py
But running cp
from the command line on the same file to the same destination doesn't cause an error. Why?
The operation that is failing is
chmod
, not the copy itself: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, useshutil.copyfile(src, dst)
, orshutil.copyfile(src, os.path.join(dst, os.path.basename(src)))
ifdst
is a directory.A function that works with
dst
either a file or a directory and does not copy permission bits:This is kind of a guess, but the first thing that pops out at me:
You have no trailing slash. While I'm not sure of the implementation of
shutil.copy()
, I can tell you thatcp
will act differently depending on what OS you're running it on. Most likely, on your system,cp
is being smart and noticing thatimages
is a directory, and copying the file into it.However, without the trailing slash,
shutil.copy()
may be interpreting it as a file, not checking, and raising the exception when it's unable to create a file namedimages
.In short, try this:
Arguments must be: