help with comparison

tekion tekion at gmail.com
Thu Nov 20 15:26:04 EST 2008


On Nov 19, 11:36 pm, George Sakkis <george.sak... at gmail.com> wrote:
> On Nov 19, 10:21 pm,tekion<tek... at gmail.com> wrote:
>
>
>
> > Hi,
> > Could some one take a look at the below code snipet which keep
> > failing:
>
> > import optparse
> > p = optparse.OptionParser(description="script to do stuff",
> > prog="myscript.py", ....)
> > p.add_option("-c" "--compress", help="0 is noncompress")
> > function1(options.compress)
>
> > here's what the what function definition looks like:
> > function1(zipfile) :
> > if (zipfile == 1):
> >    do stuff here with for compress file
> > else
> >    do stuff here
>
> > when I call the script "myscript.py 1", the above test keeps falling
> > to the else clause.  I am thinking the object zipfile is not the same
> > as "1". Any thoughts as how I should test if the argument being pass
> > in and parse by optparse is 1 or "0"?  Thanks.
>
> 1 (without quotes) is not the same as "1" (with quotes); the first is
> an integer, the second a string. optparse returns strings by default,
> so the easiest change would be to make the check 'if zipfile == "1"'.
>
> Even better, since it's a boolean option, pass action="store_true" to
> p.add_option(). The test then is reduced to "if zipfile" and the
> program is to be called by "myscript.py -c". Read the docs [1] for
> more details.
>
> HTH,
> George
>
> [1]http://docs.python.org/library/optparse.html#standard-option-actions

Thanks. This fixed it.



More information about the Python-list mailing list