Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

catching an IOError in python

I wrote a method that does some stuff and catches bad filenames. what should happen is if the path doesn't exist, it throws an IOError. however, it thinks my exception handling is bad syntax... why??

def whatever():
    try:
        # do stuff
        # and more stuff
    except IOError:
        # do this
        pass
whatever()

but before it even gets to calling whatever(), it prints the following:

Traceback (most recent call last):
  File "", line 1, in 
  File "getquizzed.py", line 55
    except IOError:
         ^
SyntaxError: invalid syntax

when imported...help?!

like image 810
tekknolagi Avatar asked Feb 02 '11 00:02

tekknolagi


People also ask

How do I get IOError in Python?

Practical Data Science using PythonIt is an error raised when an input/output operation fails, such as the print statement or the open() function when trying to open a file that does not exist. It is also raised for operating system-related errors.

What is catching an exception in Python?

Catching Exceptions in Python In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause.

How do you accept a value error in Python?

To resolve the ValueError in Python code, a try-except block can be used. The lines of code that can throw the ValueError should be placed in the try block, and the except block can catch and handle the error.

How do you fix a type error in Python?

How to Fix TypeError in Python. To avoid type errors in Python, the type of an object should be checked before performing an operation. This can help ensure that the object type is appropriate for the operation and if the operation is supported by the object.


2 Answers

there's 1 more possible if you're privileged to have an older installation

and

you're using the 'as' syntax:

     except IOError as ioe:

and

the parser's getting tripped up on 'as'.

Using as is the preferred syntax in Python 2.6 and better.

It's a syntax error in Python 2.5 and older. For pre-2.6, use this:

     except IOError, ioe:

like image 87
Der Schley Avatar answered Sep 27 '22 19:09

Der Schley


Check your indenting. This unhelpful SyntaxError error has fooled me before. :)

From the deleted question:

I'd expect this to be a duplicate, but I couldn't find it.

Here's Python code, expected outcome of which should be obvious:

x = {1: False, 2: True} # no 3

for v in [1,2,3]:
  try:
      print x[v]
  except Exception, e:
      print e
      continue
I get the following exception: SyntaxError: 'continue' not properly in loop.

I'd like to know how to avoid this error, which doesn't seem to be 
explained by the continue documentation.

I'm using Python 2.5.4 and 2.6.1 on Mac OS X, in Django.

Thank you for reading
like image 43
Brian M. Hunt Avatar answered Sep 27 '22 20:09

Brian M. Hunt