Python Tutorial: How To Check if a File or Directory Exists

► Master intermediate and advanced Python techniques with bitesized examples

A tutorial video on how to find out whether a file (or directory) exists using Python built-ins and functions from the standard library.

“How to Check if a File Exists in Python” (written tutorial) →

The ability to check whether a file exists on disk or not is important for many types of Python programs:

Maybe you want to make sure a data file is available before you try to load it, or maybe you want to prevent overwriting an existing file. The same is true for directories—maybe you need to ensure an output folder is available before your program runs.

In Python, there are several ways to verify a file or directory exists using functions built into the core language and the Python standard library.

In this tutorial you’ll see three different techniques for file existence checks in Python, with code examples and their individual pros and cons.

Option #1: os.path.exists() and os.path.isfile()

The most common way to check for the existence of a file in Python is using the exists() and isfile() methods from the os.path module in the standard library.

Option #2: open() and try…except

Another straightforward Python algorithm for checking whether a file exists: You simply attempt to open the file with the built-in open() function. Then you can look out for IOError exceptions like FileNotFoundError or PermissionError:

“FileNotFoundError: [Errno 2] No such file or directory: …”


“PermissionError: Access is denied:”

Option #3: pathlib.Path.exists() (Python 3.4+)

Python 3.4 and above include the pathlib module that provides an object-oriented interface for dealing with file system paths. Using this module is much nicer than treating file paths as simple string objects.

What’s the preferred way to check if a file exists using Python?

In most cases where you need a file existence check I’d recommend you use the built-in pathlib.Path.exists() method on Python 3.4 and above, or the os.path.exists() function on Python 2.

However, there’s one important caveat to consider:

Keep in mind that just because a file existed when the check ran won’t guarantee that it will still be there when you’re ready to open it:

While unlikely under normal circumstances, it’s entirely possible for a file to exist in the instant the existence check runs, only to get deleted immediately afterwards.

To avoid this type of race condition, it helps to not only rely on a “Does this file exist?” check. Instead it’s usually better to simply attempt to carry out the desired operation right away. This is also called an “easier to ask for forgiveness than permission” (EAFP) style that’s usually recommended in Python.

Watch the full tutorial for details or check out for a written tutorial covering the topics of this video.

FREE COURSE – “5 Thoughts on Mastering Python”


* * *

► Python Developer MUGS, T-SHIRTS & MORE:

FREE Python Coding Tutorials & News:
» Python Tutorials:
» Python News on Twitter:
» Weekly Tips for Pythonistas:
» Subscribe to this channel:


Xem thêm bài viết khác:


  1. Thanks man, this helped me solve my issue!

  2. This comment was made by AI

  3. May I know how to set up the terminal color scheme and the auto filling for python in terminal?

  4. Hi, your video is really helpful. Apart from this topic, Can you please give me some insights about what is .action file and how to open it. I am downloading files from a link (windows- browser chrome) and it is downloading as .action. it is an excel file in real so when I change extension to .XLS I am able to open it but an exception is thrown in ms Excel. I am not sure how to deal with this pythonic way.

  5. Hi Dan

    Like your video, thanks
    is it possible to check multiple files in one directory?

  6. Hi Dan, what kind of IDE u use. I am using Pycharm and i dont got displayed the [ython functions with all the explanations in it. Thank you for your videos!

  7. Hi Dan that was really informative. Very neat presentation. Like your videos so much. Thanks lot

  8. Hi Mr. Bader… I'm really enjoying your videos. I've learned a lot of interesting ideas you've presented.

    Your teaching style (in a way) reminds me of Raymond Hettinger (Python dev). He has excellent ways of teaching python in a simplified way, as well as how to simplify code in general.

Leave a Reply

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