EN
Python - check if file is directory
0
points
In this article, we would like to show you how to check if the file is a directory in Python.
Quick solution:
import os.path
is_directory = os.path.isdir("example.txt")
print(is_directory) # False
Practical example
In this example, we check if the example.txt
file is a directory using isdir()
method from os.path
module.
import os.path
is_directory = os.path.isdir("example.txt")
print(is_directory) # False
if the file isn't directly in our project we need to specify the path:
import os.path
is_directory = os.path.isdir("C:\\example_directory\example.txt")
print(is_directory) # False