tags:

views:

96

answers:

2

Ok, python newbie here.

This is so elementary, I am almost embarrassed to post the question - yet still, I have spent more than a few minutes trying to get it to work - time to swallow my pride.

Here is the script. Can anyone spot why it is not printing the passed arguments?

import sys, getopt

def usage():
    print 'Unknown arguments'

def main(argv):
    try:
        opts, args = getopt.getopt(argv,'fdmse:d',['files=','data-source=','mode=','start','end'])

    except getopt.GetoptError:
        usage()
        sys.exit(999)

    for opt, arg in opts:
        # print opt,arg 
        if opt in('-f','--files'):
            print 'files: ', arg  #

if __name__ == "__main__":
    main(sys.argv[1:])

When I run the script at the command line and pass the arguments -f=dummy.csv, usage() seems to be invoked instead - WHY?

BTW, I find the logic of the program flow a bit weird (I copied it from here). Normally, I would have thought that the logic will be implemented in the try branch, and then AFTER that comes the exception handler.

Is this (as pasted in the code above) the 'Pythonic' way to write try/catch blocks?

A: 

Normally, I would have thought that the logic will be implemented in the try branch

"Normally"? What does normally mean?

What is the program supposed to do? What exceptions make sense? What does the program do in response to the exceptions.

There's no "normally". Any more than there's a normal assignment statement or a normal function definition.

Your program does what makes sense to achieve the required end-state. There's no "normally".

S.Lott
Normally, as in C++, C# and Java. In all of those languages, the logic is within the try block, and the exception (if its thrown), is handled later. My background (as you may have guessed) is mostly C++ (and the other languages mentioned), so I have been pretty much used to handling exceptions in the style I described - if this is not the Pythonic way of doing things, its not a problem - I just wanted to know the Pythonic way of handling exceptions
skyeagle
Please don't keep saying "normally". That's a *design decision* that you make, based on what the thing is supposed to do and what exceptions may happen and what it's supposed to do in response to the exceptions. You don't throw try blocks around randomly. And there's no "normally". You **must** actually think and design the try blocks. Even in C++.
S.Lott
Hmm, at the risk of deviating from the original question - there is only one way to write try/catch[/finally] blocks in all of the languages I mentioned - thats why I used the word 'Normally'. I have never seen a try/catch statement where the exception handler appears before the logic that could throw an exception. Just saying ...
skyeagle
@ekyeagle: What? Are you confused by which statement might raise the `getopt.GetoptError`? Is that your real question? If so **update** your question. And be sure to print the exception that's raised as part of that update.
S.Lott
I see the point you're making. The exception handler is AFTER the statement that could throw the exception. Mea culpa. All is well again
skyeagle
A: 

Although this is not totally related to the topic but using argparse will be better and more user friendly (Since you are newbie).

http://docs.python.org/library/argparse.html#module-argparse

tushartyagi
@tushatyagi: Prob should have mentioned that I am using Python 2.6. IIRC, argparse is new for 2.7. I'll check again to be sure though
skyeagle
optparse has its pitfalls, but it's also a good module.
tokland