Open File Error Python
Contents |
you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. syntax for generic except clause in python Syntax Errors¶ Syntax errors, also known as parsing errors, are perhaps
Python Print Exception Message
the most common kind of complaint you get while you are still learning Python: >>> while True python custom exception print('Hello world') File "
Name Of Errors In Python
to look in case the input came from a script. 8.2. Exceptions¶ Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal: you will soon learn how to handle them in Python programs. Most exceptions are not handled by programs, however, and result in error messages as shown here: >>> 10 * (1/0) Traceback (most recent call last): File " here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings an exception can be in python and policies of this site About Us Learn more about Stack Overflow syntax for raise clause in python the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it https://docs.python.org/3/tutorial/errors.html only takes a minute: Sign up Python's “open()” throws different errors for “file not found” - how to handle both exceptions? up vote 28 down vote favorite 6 I have a script where a user is prompted to type a filename (of a file that is to be opened), and if the file doesn't exist in the current directory, the http://stackoverflow.com/questions/15032108/pythons-open-throws-different-errors-for-file-not-found-how-to-handle-b user is prompted again. Here is the short version: file = input("Type filename: ") ... try: fileContent = open(filename, "r") ... except FileNotFoundError: ... When I tested my script on my MacOS X in Python 3.3x it worked perfectly fine when I type the wrong filename on purpose (it executes the suite under "expect"). However, when I wanted to run my code on a Windows computer in Python 3.2x, I get an error that says that "FileNotFoundError" is not defined. So, Python 3.2 on Windows thinks "FileNotFoundError" is a variable and the programs quits with an error. I figured out that Python 3.2 on Windows throws an "IOError" if the input filename is not valid. I tested it on my Linux machine in Python 2.7, and it's also an IOError. My problem is now, that the code with except "FileNotFoundError": won't run on Windows's Python 3.2, but if I change it to except "IOError": it won't work on my Mac anymore. How could I work around it? The only way I can think of is to use just e here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring http://stackoverflow.com/questions/8380006/file-open-function-with-try-except-python-2-7-1 developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join http://crashcourse.housegordon.org/python-exceptions-handling-tips.html them; it only takes a minute: Sign up File Open Function with Try & Except Python 2.7.1 up vote 3 down vote favorite def FileCheck(fn): try: fn=open("TestFile.txt","U") except IOError: print "Error: File does not appear to exist." return 0 in python I'm trying to make a function that checks to see if a file exists and if doesn't then it should print the error message and return 0 . Why isn't this working??? python file function try-catch except share|improve this question edited Dec 5 '11 at 1:20 kindall 93.1k9117179 asked Dec 5 '11 at 1:14 O.rka 2,941104286 1 Specify what you mean by "not working." –kindall Dec 5 '11 at 1:21 add a comment| 3 Answers 3 active oldest clause in python votes up vote 8 down vote accepted You'll need to indent the return 0 if you want to return from within the except block. Also, your argument isn't doing much of anything. Instead of assigning it the filehandle, I assume you want this function to be able to test any file? If not, you don't need any 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 share|improve this answer edited Dec 5 '11 at 1:30 answered Dec 5 '11 at 1:17 OregonTrail 2,89121533 1 To elaborate, the problem identified by OregonTrail is that your return 0 is indented to the same level as your if statement. This puts the return outside the if, so the function returns 0 regardless of whether it got an error or not. –kindall Dec 5 '11 at 1:21 how do i call the function? do i need to set a file to a variable? if i did that it would just be opening the file . . . –O.rka Dec 5 '11 at 1:29 I've added some lines to flesh out the example –OregonTrail Dec 5 '11 at 1:30 add a comment| up vote 3 down vote I think os.path.isfile() is better if you just want to "check" if a file exists since you do not need to ac generic Exception. Use user-defined exceptions for application-level errors. Update, 2016-02-26 The kind folks at Webucator made a video based on this post. Check out the video at: https://youtu.be/T94vZmOZodY and other Webucator Online Python Classes. Disclaimer: I’m not a python fan (and certainly not an expert). Comments, feedback, suggestions and improvements are welcomed. without catching exceptions This typical python code: #!/usr/bin/env python import sys a = open("/non/existing/file","r") Will result in this output: $ ./bad0.py Traceback (most recent call last): File "./bad0.py", line 3, in Python Filenotfounderror