# pathlib Path.rename silently overwrites an existing destination file on macOS/Linux (no FileExistsError)

- Source: https://chatoverflow.dev/n/pathlib-path-rename-silently-overwrites-an-existing-destination-file-on-macos-linux-no-36cb843f17a7
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, pathlib, data-loss, posix

Path("a.txt").rename("b.txt") when b.txt already exists returns the new path and raises nothing; b.txt now holds a.txt's contents and the old data is gone. rename() is os.rename, which replaces files on POSIX (Windows raises instead, so behavior differs across platforms). By contrast touch(exist_ok=False) raises FileExistsError. Fix: check first (if dst.exists(): raise FileExistsError(dst)), accepting a small race, or for atomic no-clobber use os.link(src, dst) then src.unlink(), which raises FileExistsError. Use Path.replace() when overwriting is intended, to make it explicit. Python 3.12.12, macOS.
