To check if a path is an existing file:
os.path.isfile(path)
Return
True
if path is an existing regular file. This follows symbolic links, so bothislink()
andisfile()
can be true for the same path.
Instead of os.path.isfile
, suggested by others, I suggest using os.path.exists
, which checks for anything with that name, not just whether it is a regular file.
Thus:
if not os.path.exists(filename):
file(filename, 'w').close()
Alternatively:
file(filename, 'w+').close()
The latter will create the file if it exists, but not otherwise. It will, however, fail if the file exists, but you don't have permission to write to it. That's why I prefer the first solution.
It seems to me that all other answers here (so far) fail to address the race-condition that occurs with their proposed solutions.
Any code where you first check for the files existence, and then, a few lines later in your program, you create it, runs the risk of the file being created while you weren't looking and causing you problems (or you causing the owner of "that other file" problems).
If you want to avoid this sort of thing, I would suggest something like the following (untested):
import os
def open_if_not_exists(filename):
try:
fd = os.open(filename, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
except OSError, e:
if e.errno == 17:
print e
return None
else:
raise
else:
return os.fdopen(fd, 'w')
This should open your file for writing if it doesn't exist already, and return a file-object. If it does exists, it will print "Ooops" and return None (untested, and based solely on reading the python documentation, so might not be 100% correct).
If (when the file doesn't exist) you want to create it as empty, the simplest approach is
with open(thepath, 'a'): pass
(in Python 2.6 or better; in 2.5, this requires an "import from the future" at the top of your module).
If, on the other hand, you want to leave the file alone if it exists, but put specific non-empty contents there otherwise, then more complicated approaches based on if os.path.isfile(thepath):
/else
statement blocks are probably more suitable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With