try: with open("file.txt","r") as f: #stuff goes here except IOError: #do what you want if there is an error with the file opening