Kiểm tra file là thư mục hay file thường bằng phương thức os.path.isfile():
Ví dụ
>>> import os
>>> print(os.path.isfile('my_file.txt'))
True
>>> print(os.path.isfile('my_folder'))
False
Bổ sung: Dùng os.path.isdir(path) để kiểm tra thư mục. os.path.exists(path) kiểm tra tồn tại (file hoặc dir). Từ Python 3.4+, khuyến nghị dùng pathlib.Path:
from pathlib import Path
p = Path('my_file.txt')
print(p.is_file()) # True/False
print(p.is_dir()) # True/False
print(p.exists()) # True/False