Path.copy creates another file or tree; copied bytes and copied metadata have different defaults.
Python Path.copy: a snapshot needs an explicit metadata policy
Operation contract
A temporary receipt file is copied into an archive path, then the source is revised. The destination retains its earlier bytes. The default call checks file content only; it does not ask to preserve all available metadata. Both paths are owned by the fixture and removed when the temporary directory closes.
Failure boundary
Path.copy may replace an existing file at the destination, and symlinks are followed by default. Choose a destination policy before copying user-selected paths. The copy is not a transactional backup: source mutation during copying, failed writes, cross-filesystem behavior, and crash recovery need separate handling. Request metadata preservation only when the application requires it.
Working program
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as directory:
source = Path(directory) / "current-receipt.txt"
archived = Path(directory) / "archived-receipt.txt"
source.write_text("batch-47", encoding="utf-8")
copied = source.copy(archived)
source.write_text("batch-48", encoding="utf-8")
print("copied", copied.read_text(encoding="utf-8"))
print("current", source.read_text(encoding="utf-8"))Output
copied batch-47
current batch-48Costs and limits
Copy cost generally grows with data size, although a filesystem may support copy-on-write. Metadata preservation and cross-device handling add filesystem work; measure on the deployment volume.
Common Mistakes
- The default copy need not preserve every file attribute.
- The destination may be replaced if it already exists.
- A copy made during concurrent writes is not automatically a consistent snapshot.
