fileOpened = False while (fileOpened == False): inputFilename = raw_input ("Enter the name of the input file to read from: ") try: # If file opened OK then the entire body of the try block executes inputFile = open (inputFilename, "r") fileOpened = True except IOError: # If the file could not be opened (e.g., no file with this name at that location) # then the body of the catch executes print "Could not open file", inputFilename for line in inputFile: print line, inputFile.close()