How to check if a file exists in path using Python?

There are 2 functions available in Python to check this directly using os.path.

Try the below Python code snippet. Both functions take same parameter (file path) & return a boolean response. i.e., True if file exists or False if the file does not exist in given path.

Here is the Python code.

#Import path library in Python
import os.path
fpath = 'C:/Users/hp/Downloads/tee-1252397_640.jpg';

#Check if File exists - Method1
bfileExists = os.path.exists(fpath);
print (bfileExists);

#Check if File exists - Method2
bfileExists = os.path.isfile(fpath);
print (bfileExists);

#Print Message as per Return code
if bfileExists == True:
    print ("File Exists");
    print ("File Does not Exist");

print ("Program Completed");

These functions does not accept wild card characters as input. So, give a proper file path.

Also, this function does not check if the file can be accessed or it does not have any permission to access. These should be handled using error handling mechanism as discussed in this external link. Click here to read more about it.