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

Answer by Khaled.K for How do I check whether a file exists without exceptions?

$
0
0
import os.pathdef isReadableFile(file_path, file_name):    full_path = file_path +"/"+ file_name    try:        if not os.path.exists(file_path):            print "File path is invalid."            return False        elif not os.path.isfile(full_path):            print "File does not exist."            return False        elif not os.access(full_path, os.R_OK):            print "File cannot be read."            return False        else:            print "File can be read."            return True    except IOError as ex:        print "I/O error({0}): {1}".format(ex.errno, ex.strerror)    except Error as ex:        print "Error({0}): {1}".format(ex.errno, ex.strerror)    return False#------------------------------------------------------path = "/usr/khaled/documents/puzzles"fileName = "puzzle_1.txt"isReadableFile(path, fileName)

Viewing all articles
Browse latest Browse all 46

Trending Articles



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