all Python 3shutilfunction
shutil.copymode()
copying files
Copies the permission bits (chmod) from src onto an existing dst; contents, owner and times are left alone.
Syntax
shutil.copymode(src, dst, *, follow_symlinks=True)
dst must already exist: copymode creates nothing and copies no data — it is simply chmod by example. Only the permission bits are copied (rwx plus setuid, setgid and sticky), the owner stays the same (that is chown's job). Windows only has a read-only flag, so the effect there is barely visible. copymode does not transfer the modification time — copystat does.
Examples
import os, shutil
open('a.txt', 'w').close()
open('b.txt', 'w').close()
os.chmod('a.txt', 0o600)
shutil.copymode('a.txt', 'b.txt')
print(oct(os.stat('b.txt').st_mode & 0o777)) # → ? 0o600 — на Windows права другие
import shutil
open('a.txt', 'w').close()
try:
shutil.copymode('a.txt', 'missing.txt')
except FileNotFoundError as e:
print(type(e).__name__) # → FileNotFoundError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy