Search code examples
pythonif-statementpython-2.7optparse

Open file if 2 conditions are true, else print


I am trying to get a piece of python (2.7) code to work. The code is as followed:

import os

def parseOptions():

    import optparse
    parser = optparse.OptionParser(usage= '-h')
    parser.add_option('-t', '--type', \
                      choices= ('Warning', 'Error', 'Information', 'All'), \
                      help= 'The type of error',
                      default= 'Warning')
    parser.add_option('-g', '--goback', \
                      type= 'int')
    (options, args) = parser.parse_args()
    return options

options = parseOptions()

if options.type=='All' and options.goback=='24':
    os.startfile('logfile.htm')

else: 
    print
    print 'Type =', options.type,
    print
    print 'Go Back =', options.goback,'hours'
    print

My issue is with the if statement, it specifically needs those two options to be those exact inputs and it should open the file named lofile.htm

But at the moment it just prints as normal without opening the file.

If I remove the 'and options.goback' function leaving only 'if options.type' it opens the file. But if I have only the options.goback section it does not work either.

So my issue is with the line 'options.goback=='24':' I am just unsure of how to correct it. Because at the moment the arguments print fine, it just doesn't know what number it is.

Any help would be appreciated!

~M


Solution

  • options.goback is an int, so replace '24' by 24