4 ms·
However, the first version has a race condition. The file might not exist between the if-statement check and the call to os.remove.
by jdreaver 11y ago
However, the first version has a race condition. The file might not exist between the if-statement check and the call to os.remove.
- btilly 11y agoThat sort of issue is why the best practice is to use careful routines like https://docs.python.org/2/library/tempfile.html https://docs.python.org/2/library/tempfile.html for temp files. But in practice on a trusted server, this kind of race condition doesn't actually happen. And if you have someone untrusted on your server, you already have a bigger problem than what you're fixing by getting the race condition right. By contrast the kind of permission problem that I mentioned is common, and I've spent too many hours debugging such issues in code other people wrote. (Ever tried to run old code on a new server that someone else tried to set up? You'd be amazed how many hidden dependencies will turn up...) That is why I consider the race condition to be less important than the exception swallowing anti-pattern.