On this page
shutil.move
Prev: shutil.make_archive | Up: shutil | Next: shutil.register_archive_format
Rel: shutil.chown, shutil.copy, shutil.copy2, shutil.copyfile
Ref: docs.python.org — shutil.move
Tags: #public #python3 #stdlib #shutil
Import
import shutil
from shutil import move
Signature & Overview
shutil.move(src, dst, copy_function=<function copy2 at 0x1093e0510>)
Recursively move a file or directory to another location. This is similar to the Unix "mv" command. Return the file or directory's destination.
Usage
from pathlib import Path
import shutil
import tempfile
with tempfile.TemporaryDirectory() as tmp:
src = Path(tmp) / "old.txt"
dst = Path(tmp) / "new.txt"
src.write_text("moved", encoding="utf-8")
shutil.move(src, dst)
assert not src.exists() and dst.read_text(encoding="utf-8") == "moved"
Gotchas
- Works across filesystems: Uses fast atomic
os.rename()on the same filesystem, and automatically falls back to copy + delete across drives/mounts. - Directory destination behavior: If
dstis an existing directory,srcis moved insidedst(dst / src.name).
REPL Inspect
from shutil import move
help(move)
Linked from
Related notes
- tuts/python3/stdlib_module/shutil/make_archive Linked here; links to this page; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/register_archive_format Linked here; links to this page; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/shutil/chown Linked here; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copy Linked here; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copy2 Linked here; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copyfile Linked here; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copyfileobj shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copymode shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/copystat shared tags: #shutil, #stdlib