How to check if a file exists in Python without exceptions?
File operations are frequently used in the programs. If we try to open a file whose path doesn’t exist, an error is thrown and the program halts. To avoid this, we can first check if the file exists in the given path and then open the file. In today’s article, let’s discuss different ways to check if a file exists in Python without using the try-except/finally blocks. We will be using methods from the pathlib module and os modules. Method 1: Using pathlib.Path object Python has an in-built module named pathlib that can be used to check for any file … Read more