Quantcast
Channel: How do I check whether a file exists without exceptions? - Stack Overflow
Viewing all articles
Browse latest Browse all 46

Answer by Marcel Wilson for How do I check whether a file exists without exceptions?

$
0
0

Adding one more slight variation which isn't exactly reflected in the other answers.

This will handle the case of the file_path being None or empty string.

def file_exists(file_path):    if not file_path:        return False    elif not os.path.isfile(file_path):        return False    else:        return True

Adding a variant based on suggestion from Shahbaz

def file_exists(file_path):    if not file_path:        return False    else:        return os.path.isfile(file_path)

Adding a variant based on suggestion from Peter Wood

def file_exists(file_path):    return file_path and os.path.isfile(file_path):

Viewing all articles
Browse latest Browse all 46

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>