The report exists, but it is still being written
A process generates a report under the same name every morning. Another task reads it as soon as it appears. Everything seems coordinated until a read overlaps with a write and the result contains only part of the data.
A file's existence does not prove it is finished. When a producer writes directly over the version others read, it exposes an intermediate state. A consumer can open an empty, incomplete or inconsistent document without knowing it was still being prepared.
Prepare a separate version
A useful pattern is to generate a separate temporary file and keep the previous version available while the new one is prepared. Finish writing, close the file and validate the result first. Only then replace the version intended for readers.
Validation depends on the document. A CSV may need expected columns and a readable structure. A report may require an explicit reporting period and generation date. Do not require rows in every case: an empty result can be correct if the process explains it.
Replacement has conditions
Python's documentation explains that os.replace can replace an existing destination file when permissions allow it. It also warns that the operation may fail across different filesystems. If successful, the rename is atomic, as POSIX requires.
That property avoids exposing a half-finished rename; it does not validate the report's contents. Prepare the temporary file on the same filesystem and verify the environment's actual behaviour. Do not automatically extend this property to a synced folder or a cloud service with different rules.
Complete does not mean current
If generation fails and the previous version is retained, the document may still open perfectly. It therefore needs a visible date and a status that distinguishes a current version from an older one kept after an error.
Test an interruption during writing and a failed validation. The consumer should receive neither a partial file nor a false confirmation that it was refreshed. Also check what happens when two runs try to publish at once: atomic replacement does not decide which version is correct.
The right handoff point is not when a name appears in the folder. It is when a complete, validated and identifiable version is available for the consumer to use.