Check if a file exists in a directory

With Python there are several methods which can be used to check if a file exists, in a certain directory. When checking if a file exists, often it is performed right before accessing (reading and/or writing) a file. Below we will go through each method of checking if a file exists (and whether it is accessible), and discuss some of the potential issues with each one.

1. os.path.isfile(path)

This function returns true if the given path is an existing, regular file. It follows symbolic links, therefore it’s possible for os.path.islink(path) to be true while os.path.isfile(path) is also true. This is a handy function to check if a file exists, because it’s a simple one liner. Unfortunately the function only checks whether the specified path is a file, but does not guarantee that the user has access to it. It also only tells you that the file existed at the point in time you called the function. It is possible (although highly unlikely), that between the time you called this function, and when you access the file, it has been deleted or moved/renamed.

For example, it may fail in the following scenario:

2. os.access(path, mode)

This function tests if the current user (with the real uid/gid) has access (read and/or write privileges) to a given path. To test if the file is readable os.R_OK can be used, and os.W_OK can be used to determine if the file is writable. For example, as follows.

If you are planning on accessing a file, using this function is somewhat safer (although not completely recommend) because it also checks if you can access (reading or writing) the file. However if you plan on accessing the file, it is possible (although unlikely), that in between the time you check it is accessible and the time you access it, it has been deleted or moved/renamed. This is known as a race condition, and should be avoided. The following is an example of how it can happen.

3. Attempting to access (open) the file.

In order to absolutely guarantee that the file not only exists, but is accessible at the current time, the easiest method is actually attempting to open the file.

This can be transformed into an easy to use function, as follows.

For example, you can use it as follows:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.