shutil.copy()引发了权限错误:
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'
Run Code Online (Sandbox Code Playgroud)
copy-test.py:
import shutil
shutil.copy('src/images/ajax-loader-000000-e3e3e3.gif', 'bin/styles/blacktie/images')
Run Code Online (Sandbox Code Playgroud)
我从命令行运行copy-test.py:
python copy-test.py
Run Code Online (Sandbox Code Playgroud)
但是cp从同一文件的命令行运行到同一目标不会导致错误.为什么?
eca*_*mur 40
失败的操作chmod不是复制本身:
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'
Run Code Online (Sandbox Code Playgroud)
这表示该文件已存在并由另一个用户拥有.
shutil.copy指定复制权限位.如果您只想复制文件内容,请使用shutil.copyfile(src, dst),或者shutil.copyfile(src, os.path.join(dst, os.path.basename(src)))如果dst是目录.
一个与dst文件或目录一起使用的函数,不会复制权限位:
def copy(src, dst):
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))
shutil.copyfile(src, dst)
Run Code Online (Sandbox Code Playgroud)