Python Tutorial: How To Check if a File or Directory Exists
Skills:
Tool Use & Function Calling80%
Key Takeaways
This video tutorial demonstrates how to check if a file or directory exists in Python using the OS path module and pathlib module, with practical examples and code snippets.
Full Transcript
hey everyone in this tutorial you're going to learn how to check whether a file or directory exists in Python using only built-in functions and functions from the python standard Library so checking whether or not a file exists is uh very important for many kinds of python programs that you might want to write so maybe you want to check and make sure that a data file is available before you actually load it or maybe you want to make sure you're not overwriting an existing file and the same could be true for directories so in Python there's several ways to verify whether a file or directory exists and in this tutorial I'm going to cover three different methods all right so I'm in my terminal here and you can see that the directory that I'm in at the moment this one that I created here file exists it's completely empty there's no files in there so what I'm going to do now is I'm going to create a simple file I'm just going to go and I'm going to create a file called test.txt and we can check that so now there's a test.txt file and I'm also going to create an empty directory for us to test so I'm just going to call that test. D and now you can see we have a file in here test.txt and a folder or directory called test dder and really this is all we need to go through these three different approaches for testing file or directory exists and um so what I'm going to do now is I'm going to launch into my python interpreter so I'm running this on Python 3 most of this will actually work on python 2 and I'll you know mention it in the tutorial and if there's something you may want to change in order to get it to work so the first approach that we're going to take is the OS path module so this is a module that's built into the python standard Library I'm going to import it it's the os. path module and os. path includes some interesting functions that we can use to check whether file or directory exists so the first function here is the exists function and you can see here I'm uh I'm using this uh this python interpreter called B python I have a video on that as well if you're interested that shows me some autocompletion information and so I can see here that we can use this function to test whether a file exists or not so let's just try it out so I'm going to go in here and say this file doesn't exist you know just give it a name where I'm sure that there's no file under that name in the current directory so when I run this OS path. exist tells me hey the file doesn't actually exist now let's do the same thing with a file that actually exists so got this file test.txt that I created previously and now when I call O path exists I'm going to get true as an answer and the same thing is also going to work for directories so I can go in here so I can go in here and say oath. exists tester and then it's going to tell me hey this directory actually exists right so this works for both files and directories now in some cases you may want to make a distinction between a directory and a file so in order to do that there is also the OS path do is file function and that's going to tell you whether something exists and it's also a file so I can go in here and give it something give it a path that doesn't exist and it tells me false then I can go in here and say hey does test.txt exist and is it a file yes it is and then I can also ask it the same question about the test directory that I created in this case well it exists as you've seen before but it's not a file so this is why the is file function is going to return false and so this is a pretty good approach this is the first option for checking whether a file or directory exists in Python however it doesn't actually verify whether you can access the file so it only ensures that the file exists at that very moment in time but it doesn't actually make sure that your program or the user the program runs under has access to the file and it could actually open it so we're going to tackle that problem next so now we're going to talk about what you can do to actually make sure a file is accessible to your program as well for that technique to work what we're going to do is well we're just going to try and open the file so if I try and open a file that does not exist you can see here that I'm getting a file not found error and so we're getting some feedback through through that exception mechanism so what we can do is we can use this feedback from the exception to check whether a file exists and whether we can actually open it whether our program has the necessary permissions to actually open the file and so the way you do that is with a try accept block so what I'm going to do here is I'm going to start a try block and then I'm going to say all right so open the file doesn't exist. txt is not going to exist and then actually if you succeeded we'll just close it again and if we failed on a file not found error and then we can just say the file does not exist so let's run this okay so this printed the file does not exist and you can see exactly why because trying to open the file it raised a file not found error and then we caught that error and actually turned it into a message that we printed out to the console now the only thing this does up until now is it checks whether a file exists and if the file exists but we're not allowed to open it this is still going to Ras an exception so this is going to rase an IO error exception the good thing about this is that we can actually leverage this exception or use this exception to determine whether a file is also accessible to us so let's talk a little bit more about this whole idea of checking whether our file is actually accessible so what I'm going to do here is I'm going to leave my interpreter so you can see here we have this not allowed txt file and it belongs to my user so what I'm going to do now is I'm going to take away my access rights so I'm going to say um going to take away the right to read this file for for everyone and so you can see here so that changed the permission Flags here and now when I try to access this file I'm going to get a permission denied error now we can take a look what that looks like that particular error how that gets represented in Python so I can go in here now and I can say okay open this not allowed file and we're getting a permission error so that's different to the file not found error that we got previously now all of these errors they are subclasses of the io error class so we can take advantage of that and actually make sure that a file is available and also accessible again using a tri accept block so I can go in here and I can say okay well you open a file open uh let's actually tried with this not allowed file if you open it we're going to close it right away and for that particular part you could also use the width statement which would automatically close the file I'm not going to cover that in this tutorial but I've got another tutorial that goes into that and shows you how to do that um because some people consider that more pythonic but um it's really a little bit out of the scope of this tutorial so what I'm going to do here is we can actually catch this other error so I could say okay file let's actually go IO error so IO error is the parent class for permission error and also the file not found error so that way I can handle them all in one I can just say file not found or not accessible right I can just Mash these two error types together and in this case we get an answer that actually matches the reality of the situation so as you can see this not allowed file um actually exists but we can't open it so if I go ahead and ask o path exists whether a file exists it's going to say yes but we still can't access it so there's a difference between being able to access a file the just the fact of whether that file or directory exists there's also the os. access function that uh does something very similar to what I've shown you here in this code snippet so you might want want to look that up as well because that could also be helpful if you don't want to write this code and of course you could always extract this snippet here into a separate function and then have a reusable function that you can use for these accessible checks or you're just going to use os. access all right then there's one more option for you to check whether a file or directory exists in Python and that works using the path lib module so this is a relatively new module that was added to the python standard library in Python 3.4 and so it's only available on Python 3 however there's a way you can install it as a thirdparty module that makes it available on python 2 as well but probably a good idea to to use it on Python 3 and up um and this is a cleaned up version of many of the things that you find in the OS path module so what it does at its core is it provides an objectoriented interface to these uh to dealing with paths and um files on the file system which is kind of neat so here I can go and create a new path object and then I have this path object and I can query it so I can say hey does this path actually exist yes it does is it a file yes it is and of course I can also so do that with another path so I could say um I could say doesn't exist. txt and we probably want to assign that to something path 2 and so now I can go path 2. exists in this case it's going to tell me hey this other file that you want to this other path you path you were asking me about it doesn't actually exist so this is an object oriented way to work with the functionality that's available in the OS path module also so the path lib module provides a cleaner and objectoriented interface for working with the file system which can be nice so previously we're just you know dealing only with strings that were represent these paths but now with path lip we actually have objects that represent a path and we can query these objects so this could be a way to abstract away some of that functionality in your own programs and to make sure that you know 100% a certain object is actually a path and it's not just a string so this might sound like a very small and minor benefit but in a more complex program this could be actually really helpful also for long-term maintainability so let's do a quick recap here so we've covered three options for finding out whether a file exists in python or whether a directory exists in Python and then also we specialized that a little bit more and uh looked into whether a file is actually accessible to your program so the first option we looked into was using the os. path. exist function and that was purely for existence checks and the second option we looked into was using the open function and a tri accept statement with uh IO error and file not found error and we we were able to use that to check for the existence of a file and to also check for accessibility can our program actually access the file so this is another thing that we've tried and then the third approach was to use path li. paath and then calling the exists function on that so you've just seen these three different approaches for checking whether or not a file exists in Python and before you leave I want to give you um a quick caveat for these file existence checks so when you're running a file existence check with os. path. exists for example you're getting an answer for that particular instant where this exist function Ran So if at that particular moment a file exists then of course Python's going to tell you hey this file exists right now now this doesn't guarantee that the file still exists at a later point in time because someone else could just go in in while the program is running and maybe they're deleting that exact file at this exact instant this might be fairly unlikely but um if you run your program often enough maybe hundreds or millions of times eventually something like that's going to happen right and this is called a race condition we're trying to open a file and in the meantime it might have been deleted or you know there's some other actor that's trying to to remove the file and the race condition is who gets there first are we going to be able to open the file before that other thing or other program can delete the file or are they going to be able to delete it first and we can't open it so this is called a eras condition and the problem with these exist checks is that usually they only give you an answer for that particular instant does the file exist right now so actually a better approach usually if you want to do something with a file is to just straight up open the file so instead of check checking whether the file exists before you open it just try and open it right away and then keep it open work with it and then close the file so that is usually the safer approach because it can help you avoid many of these race conditions and uh I've got a longer tutorial a written tutorial about this topic that I'm linking in the description so if you're interested you can learn more about this type of race condition and how you can avoid it but I just wanted to put a heads up at the end of this tutorial so that you know about some of the disadvantages off this technique or just something that you need to look out for now you might be wondering what's actually the best way to check whether a file exists or not in Python and I would say on Python 3 or 34 and above I would probably use the path li. paath exists function because I think the pathlib module is uh a really great library for working with the file system so I would probably use that on python 2 I would lean towards using OS path exist exists and I would do either one of these options or use either one of these options unless you're actually trying to avoid this race condition so if it's purely for an existence check then it's okay to use option one or option two but if you actually want to try and work with the file and not just check whether it exists then I would actually recommend that you use a modified version of the second option here where you just open the file and you work with it until you're done and then you close it and you just look out for a file not found error and a permissions error all right so a bit of a longer tutorial on how to check whether a file or directory exists in Python but you know sometimes the devil is in the details so I hope this gave you some insights and helped you solve your problem all right so if you enjoyed this tutorial and you want to continue learning python with me then subscribe to my channel and I'll chat with you next time take care
Original Description
https://dbader.org/python-tricks ► 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) → https://dbader.org/blog/python-check-if-file-exists
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: ..."
or
"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 recom
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Real Python · Real Python · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A better Python REPL – bpython vs python interpreter
Real Python
Introducing large-type.com – A Utility Website
Real Python
Reading Hacker News Without Wasting Tons of Time
Real Python
Forward References and Python 3 Type Hints
Real Python
Using Sublime Text as your Git Editor
Real Python
Python Code Linting and Auto-Complete for Sublime Text
Real Python
Make your Python Code More Readable with Custom Exceptions
Real Python
Write Better Tests with Sublime Text's Split Layout Feature
Real Python
How to Use Sublime Text from the Command Line
Real Python
Rename Variables with Multiple Selection in Sublime Text
Real Python
Sublime Text Settings for Writing PEP 8 Python
Real Python
Write Cleaner Python with Sublime Text's Indent Guides
Real Python
Sublime Text Whitespace Settings for Python Development
Real Python
Function Argument Unpacking in Python
Real Python
Python Code Review: Debugging and Refactoring "Conway's Game of Life" + Automated Tests
Real Python
Using "get()" to Return a Default Value from a Python Dict
Real Python
A Python Shorthand for Swapping Two Variables
Real Python
Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Real Python
Click & Jump to Test Failures from the Command Line (iTerm2)
Real Python
Setting up Sublime Text for Python Developers
Real Python
Sublime Text + Python Guide Overview
Real Python
Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Real Python
Type-Checking Python Programs With Type Hints and mypy
Real Python
A Shorthand for Merging Dictionaries in Python 3.5+
Real Python
Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Real Python
My Python Code Looks Ugly and Confusing – Help!
Real Python
Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Real Python
Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Real Python
Programmer Portfolio – Example and Walkthrough
Real Python
How to Get Your 1st Speaking Gig at a Tech Conference
Real Python
How to Build Your Public Speaking Skills as a Developer
Real Python
The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
Real Python
Setting up Sublime Text for Python Developers – Lesson #1
Real Python
Cool New Features in Python 3.6
Real Python
"is" vs "==" in Python – What's the Difference? (And When to Use Each)
Real Python
Emulating switch/case Statements in Python with Dictionaries
Real Python
Python Function Argument Unpacking Tutorial (* and ** Operators)
Real Python
What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
Real Python
A Crazy Python Dictionary Expression ?!
Real Python
String Conversion in Python: When to Use __repr__ vs __str__
Real Python
Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Real Python
Optional Arguments in Python With *args and **kwargs
Real Python
Python Context Managers and the "with" Statement (__enter__ & __exit__)
Real Python
Installing Python Packages with pip and virtualenv / venv
Real Python
"For Each" Loops in Python with enumerate() and range()
Real Python
Python Code Review: LibreOffice Automation and the Python Standard Library
Real Python
Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Real Python
Python Tutorial: List Comprehensions Step-By-Step
Real Python
Leveraging Python's Implicit "return None" Statements
Real Python
What's the meaning of underscores (_ & __) in Python variable names?
Real Python
Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Real Python
Writing automated tests for Python command-line apps and scripts
Real Python
How to find great Python packages on PyPI, the Python Package Repository
Real Python
Immutable vs Mutable Objects in Python
Real Python
PyPI vs Warehouse, the Next-Generation Python Package Repository
Real Python
pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
Real Python
My Experience at PyCon 2017 in Portland
Real Python
Pylint Tutorial – How to Write Clean Python
Real Python
"Reverse a List in Python" Tutorial: Three Methods & How-to Demos
Real Python
Python Refactoring: "while True" Infinite Loops & The "input" Function
Real Python
More on: Tool Use & Function Calling
View skill →Related Reads
🎓
Tutor Explanation
DeepCamp AI