Check if a file exists using Python

How do I check if a file exists, using Python, without using a try statement?
Last updated:5/11/2015 8:20:54 AM

1 Answers

Mayank Tripathi
Mayank Tripathi

You can also use os.path.isfile

Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.
import os.path

You have the os.path.exists function:

import os.path
This returns True for both files and directories.

Use os.path.isfile to test if it's a file specifically