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 very important for many kinds of Python programs that you might want to write so. Maybe you want to check and make sure that the data file is available before you actually load it, or maybe you want to make sure you’re not overriding an existing file and the same could be true for directories, so in Python, there’s several ways to verify whether our file or directory exists and in this tutorial, I’m going to cover three different methods. Alright, 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 Dot TXT and we can check that. So now there’s a test dot 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 dot there, and now you can see. We have a file in here. Test, TXT and a folder or directory called test. – there it, really. This is all we need to go through these three different approaches for testing whatever file or directory exists and 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 Ill, 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 dot have module, and, Oh, star 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 using this. This Python interpreter called B Python. I have a video on that as well. If you’re interested that shows me some auto-completion 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 write 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 I’ve got this file Test Dot. TXT that I created previously, and now when I call, oh is 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 OS Path Dot exists. Test there, 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 Dot ace 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 tells me false, then. I can go in here and say Payta’s testtxt exists. And is it a file? Yes, it is, and then you can also ask it the same question about the test directory data created in this case. Well, it exists as you’ve seen before, but it’s not a file. So this is why the if file function is going to return false and so this is a pretty good approach 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 I 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. 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 except 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. This 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 lets. Run this, okay, So this printed the file does not exist and you can see exactly why because trying to open the file it race, they found that found error and then we caught that error. It actually turned it into a message that we printed out to the console. Now the only thing this does 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 race an exception, so this is going to erase an I/O 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 to 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 I’m going to take away the right to read this file for everyone, and so you can see here so that change the permission flags here, and now when I try to access this file? I’m going to get a permission denied error. So 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. Err, so that’s different to the file, not found error that we got previously now all of these errors, their subclasses off 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 try except block. So I can go in here and I can say, okay. Well, you open the file open. Actually, try it with this. Not a loud file. If you open it or close it right away. And for that particular part, you could also use the with 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 because some people consider that more pythonic, but it’s really a little bit out of the scope of this tutorial, so 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. All 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 is not a loud file, It actually exists, but we can’t open it, so if I go ahead and ask, always path exists whether the file exists is 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 dot access function that does something very similar to what I’ve shown you here in this code snippet, so you might 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, it 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 Dot 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 in Python 3 however, it is a way you can install it as a third party module that makes it available in Python 2 as well, but probably a good idea to to use it on Python 3 and up 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 object-oriented interface to these to dealing with paths and 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, this path actually do this. Yes, it does file. Yes, it is, and, of course. I can also do that with another path, so I could say I could say it. Doesn’t exist, TXT and probably one that to something path to. And so now I can go path to Dot exists in this case is going to tell me. Hey, this other file that you want to this other path, your path, you are asking you 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 object-oriented interface for working with the file system, which can be nice so previously were. Just, you know, dealing only with strings that would 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. 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 looked into whether a file is actually accessible to your program. So the first option we looked into was using the OS path dot exists function and that was purely for existence checks and the second option we looked into was using the open function and a try accept statement with IO error and file not found error and 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 lead path and then calling the exists function on that 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 a quick caveat for these file existence checks, so when you’re running a file existence check with OS Path dot exists, for example, you’re getting an answer for that particular instant where this exists. Function ran. So if at that particular moment of file exists, then, of course, Python is 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 while the program is running, and maybe they’re deleting that exact file. Have this exact instant. It’s might be fairly unlikely, But if you run your program often enough, maybe hundreds or millions of times, eventually, something like that is going to happen, right, and this is called a race condition Were 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 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 at first? And we can’t open it? So this is called a race condition now. The problem with these exists 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 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. I just wanted to put a heads up at the end of this tutorial so that you know about some of the disadvantages of 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 3 4 and above I would probably use the path. Lib path exists function Because I think the path. Lib module is a really great library for working with the file system, so I probably use that on Python, too. I would lean towards using OS path 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 1 or Option 2 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, it’s a bit of a longer tutorial on how to check whether a file or directory exists in python. But you know, sometimes the Devils 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!