Python Glossary Русский

all Python 3shutilfunction

shutil.copystat()

copying files

Transfers src's metadata onto an existing dst: permission bits, access and modification times, flags. It does not copy the contents.

Syntax

shutil.copystat(src, dst, *, follow_symlinks=True)

It is the second half of copy2(): copy2 = copyfile + copystat. Owner and group are not copied — an ordinary user cannot change them anyway — and Windows creation time or macOS resource forks are lost too, so a perfect metadata copy does not exist. Use it when your own code wrote the file and it should look like the original, for example to keep mtime for make or rsync.

Examples

import os, shutil
open('a.txt', 'w').close()
open('b.txt', 'w').close()
os.utime('a.txt', (0, 1_000_000))
shutil.copystat('a.txt', 'b.txt')
print(os.stat('b.txt').st_mtime)  # → 1000000.0
print(os.path.getsize('b.txt'))  # → 0 — данные не копируются

See also

Open in the full glossarydocs.python.orgReport an inaccuracy