In this post, you shall learn if a given path is a file or a directory with the help of the Python programming language.
Prerequisite:
Methods definition:
# isfile() method
os.path.isfile(path)
# "path": the file path.
# returns True if "path" is an existing regular file.
# isdir() method
os.path.isdir(path)
# "path": the directory path.
# returns True if "path" is an existing directory.
The script:
import os
file = '/home/User/Desktop/test.txt'
dir = '/home/User/Desktop/'
isFile = os.path.isfile(file)
print(isFile) # True
isFile = os.path.isfile(dir)
print(isFile) # False
isDir = os.path.isdir(dir)
print(isDir) # True
isDir = os.path.isdir(file);
print(isDir) # False