#How do I check if a file exists?

17 messages · Page 1 of 1 (latest)

mossy roost
#

nightly tho

#

this one is on stable

vast nexus
#

why do you want to know whether a file exists?

#

because if you're writing something like "if file exists then open it", which 99% of people do when they ask this question, that's a race condition

mossy roost
#

you can open it with a create if not already existing flag

vast nexus
#

that's a race condition, what if the file gets created in the time between you checking and creating it?

mossy roost
vast nexus
#

in this case it might be a bit silly, but stuff like this has been a cause of security vulns in the past

mossy roost
vast nexus
#

probably create_new

#

i mean, you can do what you want. I'm just pointing out it's bad form and better methods exist 😛

dreamy reef
#

Another process could create it

#

Or the user could, too

#

As a general rule, you should never to "check then do" with files. That's susceptible to race conditions.