EN
Python - check if file is regular file
0 points
In this article, we would like to show you how to check if the file is a regular file in Python.
Quick solution:
xxxxxxxxxx
1
import os.path
2
3
is_file = os.path.isfile("example.txt")
4
print(is_file) # True
In this example, we check if the example.txt
file is a regular file using isfile()
method from os.path
module.
xxxxxxxxxx
1
import os.path
2
3
is_file = os.path.isfile("example.txt")
4
print(is_file) # True
if the file isn't directly in our project we need to specify the path:
xxxxxxxxxx
1
import os.path
2
3
is_file = os.path.isfile("C:\\example_directory\example.txt")
4
print(is_file) # True