Reading the answer in this question: How do I check whether a file exists using Python?, where the answer states this:
If the reason you're checking is so you can do something like
if file_exists: open_it()
, it's safer to use atry
around the attempt to open it. Checking and then opening risks the file being deleted or moved or something between when you check and when you try to open it.If you're not planning to open the file immediately, you can use
os.path.isfile
I couldn't understand why checking (through os.path) and then opening risks the file being moved or deleted.
What exactly does this mean?