Is there a file at a path?

EasyOS Module Programs · 11 of 49

Write a Python program to check whether a file exists at a given path.

Advertisement
Output
sample.txt True
missing.txt False
. False

Approach

  1. os.path.isfile() is True only for existing files.

Solution

Python
import os open("sample.txt", "w").close()for p in ["sample.txt", "missing.txt", "."]:    print(p, os.path.isfile(p))
Output
sample.txt True
missing.txt False
. False

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Leave a Comment