How to check if a path is a file in Python

1 min

If you are manipulating files in Python you will reach a point where checking if the path is either a folder or a file.

Usually, files have extensions, but sometimes it might happen that your file doesn't have an extension.

Here is how to check if this file is a file and not a folder

import os

print(os.path.isfile("my_file"))
The test of my_file.txt

Here you are! You now know how to check if it is a file in Python.

More on fundamentals

If you want to know more about Python fundamentals without headaches... check out the other articles I wrote by clicking just here:

Fundamentals - The Python You Need
We gathered the only Python essentials that you will probably ever need.