Python File Error Checking
Contents |
you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. python exception message Syntax Errors¶ Syntax errors, also known as parsing errors, are perhaps python raise custom exception the most common kind of complaint you get while you are still learning Python: >>> while True print('Hello syntax for generic except clause in python world') File "
Python Try Except Else
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 and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with
Is Nested Try Block Possible In Python
us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow name of errors in python Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up an exception can be in python Python's “open()” throws different errors for “file not found” - how to handle both exceptions? up vote 29 down vote favorite 6 I have a script where a user is prompted to type a filename (of a file that is to be https://docs.python.org/3/tutorial/errors.html opened), and if the file doesn't exist in the current directory, the 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 http://stackoverflow.com/questions/15032108/pythons-open-throws-different-errors-for-file-not-found-how-to-handle-b 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 except, which I usually don't want. python python-3.x filenotfoundexception ioerror share|improve this question edited Feb 22 '13 at 19:57 asked Feb 22 '13 at 19:48 user2015601 6 This isn't due to Mac/Windows, it's the version of Python. I would investigate 3.2/3.3 on OS X as well (and 3.3 on Windows), consult the change logs, and then revise the question/title as appropriate. –user166390 Feb 22 '13 at 19:53 add a comment| 4 Answers 4 active oldest votes up vote 38 down vote accepted In 3.3, IOError became an alias for OSError, and FileNotFoundError is a subclass of OSError. So you might try except (OSError, IOError) as e: ... This will cast a pretty wide net, and you can't assume that the exception is "file not found" without inspecting e.errno, but it may cover your use case. PEP 3151 discusses the rationale for the chang
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 developers http://stackoverflow.com/questions/2843702/python-error-checking-standard-practice 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 only takes a minute: Sign up Python Error-Checking Standard Practice up vote 10 down vote favorite 4 I have a question regarding error checking in Python. Let's say I have a function that takes a file path as an in python input: def myFunction(filepath): infile = open(filepath) #etc etc... One possible precondition would be that the file should exist. There are a few possible ways to check for this precondition, and I'm just wondering what's the best way to do it. i) Check with an if-statement: if not os.path.exists(filepath): raise IOException('File does not exist: %s' % filepath) This is the way that I would usually do it, though the same IOException would be raised by Python if the file does not python file error exist, even if I don't raise it. ii) Use assert to check for the precondition: assert os.path.exists(filepath), 'File does not exist: %s' % filepath Using asserts seems to be the "standard" way of checking for pre/postconditions, so I am tempted to use these. However, it is possible that these asserts are turned off when the -o flag is used during execution, which means that this check might potentially be turned off and that seems risky. iii) Don't handle the precondition at all This is because if filepath does not exist, there will be an exception generated anyway and the exception message is detailed enough for user to know that the file does not exist I'm just wondering which of the above is the standard practice that I should use for my codes. python error-handling assert share|improve this question asked May 16 '10 at 12:25 chaindriver 2621414 add a comment| 4 Answers 4 active oldest votes up vote 13 down vote If all you want to do is raise an exception, use option iii: def myFunction(filepath): with open(filepath) as infile: pass To handle exceptions in a special way, use a try...except block: def myFunction(filepath): try: with open(filepath) as infile: pass except IOError: # special handling code here Under no circumstance is it preferable to check the existence of the file first (option i or ii) because in the time between when the check or assertion occurs and when python