Overwriting is a difficult problem.
try/except a number of times, and hope that one of the times latches on to the file when no one else is.
retries = 3
# cleanup code here
if retries == 0:
raise Exception("Could not cleanup")
retries -= 1
But you would still run the risk of corrupt data, where someone somewhere ignores the "in use" status of this file and writes to it at the same time anyway, or if someone reads from it during being written to and breaks writing.
The safest option I can think of is to (1) version your files as opposed to overwriting them (
myfile_v001.obj). Another option to reduce the risk of corruption might be to (2) use symlinks.
A symlink would be small enough to be "instantly" replaced and therefore run a smaller risk of corruption, but more importantly your users would not actually be reading from this file for the majority of time, but rather from the file it points to (which could be a version).
Overwriting the symlink then, while someone else is reading from the target file, would not affect those users and should be more safe (but probably still not entirely safe, I'm not sure).
Hope it helps!