try... except SyntaxError: unexpected EOF while parsing

skip at pobox.com skip at pobox.com
Wed Apr 4 13:56:28 EDT 2007


(Are you Howard the Duck's lesser known cousin?)

>>>>> "oscar" == oscartheduck  <oscartheduck at gmail.com> writes:

    oscar> I have a small script for doing some ssh stuff for me. I could
    oscar> have written it as shell script, but wanted to improve my python
    oscar> skills some.

    oscar> RIght now, I'm not catching a syntax error as I'd like to.

Well, you should probably never catch SyntaxError (unless you use exec or
eval, both of which you should also probably never use), however...  What
you're describing is not a Python syntax error.  In addition, if you're
expecting to catch problems with the call to input("Please ...") you'll need
to have it within the try block.

Given that port numbers have to be ints I'd try something like:

    port = 2024
    userport = raw_input("Please enter a port #: ").strip()
    if userport:
        try:
            port = int(userport)
        except ValueError:
            pass
    if port > 65535:
        blah blah blah
    ...

Note that input("prompt") is equivalent to eval(raw_input("prompt")), so you
should probably never use it.  (It's going away in Python 3.0 anyway.)

Skip



More information about the Python-list mailing list