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

Total Post:156

Points:1096
Posted on    May-11-2015 12:18 AM

 Python File  Python 
Ratings:


 1 Reply(s)
 525  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

Points:3117
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
os.path.isfile(fname) 


You have the os.path.exists function:

import os.path
os.path.exists(file_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