If the reason you're checking is so you can do something like if file_exists: open_it(), it's safer to use a try 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
Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.
if you need to be sure it's a file.
Starting with Python 3.4, the pathlib module offers an object-oriented approach (backported to pathlib2 in Python 2.7):
from pathlib import Path
my_file = Path("/path/to/file")
# file exists
To check a directory, do:
# directory exists
To check whether a Path object exists independently of whether is it a file or directory, use exists():
# path exists
You can also use resolve() in a try block:
my_abs_path = my_file.resolve():
# doesn't exist
The official backport is pathlib2, pathlib module isn't maintained anymore.
os.path.isfile(file_path) and Path(str(file_path)).is_file()
I totally agree that there is little reason to simply check for presence of a file unless the mere presence of it is a signal all by itself. In which case it would be better to have a CSV file where you write a value indicating the signal - and then can have more than one signal conveyed via a single file.
Good point with the possibility of the file being changed after the check!