Home > DeveloperSection > Forums > Check if a file exists using Python
Samuel Fernandes
Samuel Fernandes

Total Post:155

Posted on    May-11-2015 12:18 AM

 Python File  Python 

 1 Reply(s)
 485  View(s)
Rate this:
How do I check if a file exists, using Python, without using a try statement?

Mayank Tripathi
Mayank Tripathi

Total Post:397

Posted on    May-11-2015 8:20 AM

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

Don't want to miss updates? Please click the below button!

Follow MindStick