(file: StrOrBytesPath, mode: str = "w+b")
| 39 | |
| 40 | @contextmanager |
| 41 | def atomic_file(file: StrOrBytesPath, mode: str = "w+b"): |
| 42 | head, tail = os.path.split(os.fsdecode(file)) |
| 43 | with NamedTemporaryFile(mode, prefix=tail + "-", dir=head, delete=False) as f: |
| 44 | yield f |
| 45 | os.replace(f.name, file) |
| 46 | |
| 47 | |
| 48 | @dataclass |