all Python 3shutilfunction
shutil.copy2()
copying files
Copies a file and tries to preserve all metadata: permissions, access and modification times; dst may be a directory.
Syntax
shutil.copy2(src, dst, *, follow_symlinks=True) -> path
copy2 = copyfile + copystat, and it is the default copy function of copytree() and move(). 'All metadata' is an overstatement: owner and group, Windows creation time and ACLs are not preserved. Times are copied with the file system's precision, so comparing st_mtime with == across file systems (FAT, for instance) is unreliable — allow a couple of seconds of slack.
Examples
import os, shutil
open('a.txt', 'w').close()
os.utime('a.txt', (0, 1_000_000))
c1 = shutil.copy('a.txt', 'b.txt')
c2 = shutil.copy2('a.txt', 'c.txt')
print(os.stat(c1).st_mtime == 1_000_000) # → False
print(os.stat(c2).st_mtime == 1_000_000) # → True
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy