Python Glossary Русский

all Python 3shutilfunction

shutil.copy()

copying files

Copies a file together with its permission bits; dst may be a directory — then the copy goes inside it under the same name.

Syntax

shutil.copy(src, dst, *, follow_symlinks=True) -> path

copy = copyfile + copymode: permissions are transferred, but the modification time is not — the copy gets 'now'. If the date matters (backups, syncing), use copy2. It returns the path of the new file, handy when dst was a directory. It does not copy directories: a directory as src raises IsADirectoryError, use copytree for those.

Examples

import os, shutil
os.mkdir('backup')
open('a.txt', 'w').close()
os.utime('a.txt', (0, 1_000_000))
dst = shutil.copy('a.txt', 'backup')
print(dst)  # → ? backup/a.txt — разделитель зависит от ОС
print(os.stat(dst).st_mtime == 1_000_000)  # → False — время не перенесено

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy