Python Check File Exist

Aug 16, 2019

Solution 1: os.path

Check is file

import os.pathos.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

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.