Check if a File Exist With Python

In this post, we will use Python to check if a file exists on the operating system and print the result.

OS Path

To read file paths and check if a file exists on the system, we need to use the os.path module, and therefore we need to import into the code (as shown below).

To make things a bit easier, I’m using a variable to store the path, and in the last line, I’m printing the result. The output will come in the form of True or False.

from os.path import exists
file_path = "file1.py"
print(f"The file exist:{exists(file_path) }")

Leave a comment

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