You will need to defer return 0 if you want to return from the except block. Also, your argument does nothing. Instead of assigning a file descriptor to it, I assume that you want this function to be able to test any file? If not, you do not need arguments.
def FileCheck(fn): try: open(fn, "r") return 1 except IOError: print "Error: File does not appear to exist." return 0 result = FileCheck("testfile") print result
source share