Python Check File Exist

August 16, 2019

Solution 1: os.path

Check is file

import os.path

os.path.isfile(filename)

Check if file or directory exists

os.path.exists(filename)

Check if directory

os.path.isdir(filename)

Solution 2: pathlib

p = Path(filename)

p.is_file()
p.is_dir()
p.exists()

NOTE: Python 3.4 pathlib or pathlib2 for Python 2.x

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.