Python Glossary Русский

all Python 3shutilfunction

shutil.move()

directory trees

Moves a file or directory like mv: on the same disk it renames, across disks it copies and then deletes the source.

Syntax

shutil.move(src, dst, copy_function=copy2) -> dst

Unlike os.rename, move works across file systems and can move into a directory: if dst is an existing directory, src ends up inside and the new path is returned. If an entry with the same name is already there, shutil.Error is raised. An existing file at dst is silently overwritten on Unix, while on Windows it depends on os.rename and may raise an error. Across disks the copy goes through copy2, so it is slow and not atomic: an interrupted move leaves a partial copy.

Examples

import os, shutil
os.mkdir('archive')
open('report.txt', 'w').close()
print(shutil.move('report.txt', 'archive'))  # → ? archive/report.txt — разделитель зависит от ОС
print(os.listdir('archive'))  # → ['report.txt']
open('report.txt', 'w').close()
try:
    shutil.move('report.txt', 'archive')
except shutil.Error as e:
    print(type(e).__name__)  # → Error

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy