On this page
shutil.SameFileError
Prev: shutil.rmtree | Up: shutil | Next: shutil.SpecialFileError
Rel: shutil.chown, shutil.copy, shutil.copy2, shutil.copyfile
Ref: docs.python.org โ shutil.SameFileError
Tags: #public #python3 #stdlib #shutil
Import
import shutil
from shutil import SameFileError
Signature & Overview
shutil.SameFileError(...)
Raised when source and destination are the same file.
Methods & Attributes
SameFileError.add_note(self, note, /)โ Add a note to the exceptionSameFileError.with_traceback(self, tb, /)โ Set self.traceback to tb and return self.
Usage
from pathlib import Path
import shutil
import tempfile
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / "a.txt"
p.write_text("hi", encoding="utf-8")
try:
shutil.copyfile(p, p)
except shutil.SameFileError:
assert p.read_text(encoding="utf-8") == "hi"
Gotchas
- Protects against truncating a file onto itself: Raised by
copyfile(),copy(), andcopy2()whensrcanddstresolve to the same file (including via symlinks or hard links).
REPL Inspect
from shutil import SameFileError
help(SameFileError)
Linked from
Related notes
- tuts/python3/stdlib_module/shutil/rmtree Linked here; links to this page; shared tags: #shutil, #stdlib
- tuts/python3/stdlib_module/shutil/SpecialFileError 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