“Knowledge is a treasure, but practice is the key to it.” ― Lao Tzu
Python offers several alternative ways of checking whether a file exists. Each of these ways come with several quirks. Examine each and pick the one that suits you.
2. Attempt to Open the File
The simplest way is to attempt to open the file in read-only mode. An exception means the file does not exist. The following function return True or False depending on the check.
def file_exists(fname): try: with open(fname, 'r') as f: return True except IOError as x: return False
3. Suppress Exception while the Opening File
What if you want do something if the file exists, but silently move on if it does not? Suppress the exception!
For python versions earlier than 3, define the following class:
class suppress(object): def __init__(self, *exceptions): self.exceptions = exceptions def __enter__(self): pass def __exit__(self, exc_type, exc_value, traceback): if exc_type is not None: return issubclass(exc_type, self.exceptions)
For python 3 and above, just import as follows:
from contextlib import suppress
Now open the file without raising an error!
with suppress(IOError), open(sys.argv, 'r') as f: print f.readlines()
In my opinion, this is about as clean a solution as any.
4. Check for File Access using access()
Quite obvious, isn’t it? Check for file access using os.access(). The advantage is you can check for read, write or execute as required. And no need for dealing with exceptions, either.
print os.access(sys.argv, os.R_OK) print os.access(sys.argv, os.W_OK) print os.access(sys.argv, os.X_OK)
Let us see some examples now (reveals a few caveats about this method).
Create an empty file. Notice that execute permission is off. (This is on Linux).
touch j python check.py j # prints True True False
Remove read permission.
chmod -r j python check.py j # prints False True False
Remove write permission.
chmod -w j python check.py j # prints False False False
And there is a gotcha! File exists, but no permissions. Keep this possibility in mind while using this method.
Here is another – does not distinguish between files, directories or links.
rm j; mkdir j python check.py j # prints True True True
Another caveat with this method is: this method uses the real uid/gid to check for permissions. This should not be a concern to you unless your program is running setuid, in which case the effective uid/gid is different from the real uid/gid.
5. Check the Stats on the File using stat()
The function os.stat() returns information about the file without opening it.
If the file exists:
print os.stat(sys.argv) # prints posix.stat_result(...)
Like open() it throws an exception, so you can suppress it if you want.
# prints OSError: [Errno 2] No such file or directory: ...
Using the class suppress as described above:
with suppress(OSError): os.stat(sys.argv) # Code from here on is executed only if the file exists. print 'file exists.'
5.1 Using lstat()
Use os.lstat() above if you do not want to be thrown off by links. If the link exists, but the link target does not, then os.stat() fails while os.lstat() returns information about the link itself.
6. Use os.path.exists()
The module os.path provides the exists() function which returns True or False as expected. Under the hood it uses stat() so the warnings in the above section apply here as well.
print os.path.exists(sys.argv) # Regular file True # Directory True # Symlink to non-existent file False # Hardlink True # Does not exist False
6.1. Maybe isfile() is more appropriate
As the examples above demonstrated, os.path.exists() merely checks for presence of the file or directory. You can also use os.path.isfile(), os.path.isdir() or os.path.islink() as per your need.
In this article, we discussed a bunch of methods to check whether a file exists in python. Starting with attempting to open a file, to using os.access() or os.stat(), we covered the common ones. Each method has certain advantages and disadvantages. Pick the one that most suits your purpose.