How do I build up an atomic file write operation? The file is to be written by a Java service and read by python scripts.
For the record, reads are far greater than writes. But the write happens in batches and tend to be long. The file size amounts to mega bytes.  
Right now my approach is:

Write file contents to a temp file in
same directory
Delete old file
Rename temp file to old filename.

Is this the right approach? How can avoid conditions where the old file is deleted but the new filename is yet to be renamed?   
Do these programming languages (python and java) offer constructs to lock and avoid this situation?
You could try and use an extra file to act as a lock, but I'm not sure if that will work out ok. (It would force you to create lock-checking and retry logic at both sides, java and python)
Another solution might be to not create files at all, maybe you could make your java process listen on a port and serve data from there rather than from a file?
Atomic file write operations (cross platform)