2

I have a set of arguments and all of their dest is 'search_and', 'search_not', 'search_start', 'search_then', and 'filename'

The code that I have to catch the error is

    if ( options.filename is None) and ( (options.search_and is None) or  (options.search_not is None) or (options.search_start is None) or (options.search_then is None):
        parser.error(usage)
        sys.exit()

It may seem like a silly mistake, can someone tell me what is going on?

EDIT#1: I added the ')' at the end to make sure it closes properly but it still says invalid syntax.

    if ( options.filename is None) and ( (options.search_and is None) or  (options.search_not is None) or (options.search_start is None) or (options.search_then is None)):

EDIT#2: Here is what I have so far.

    import optparse from OptionParser
    usage = "useage: %prog [options]"
    parser = OptionParser(usage)
    parser.add_option("-a", "--all", type="string", dest="search_and", help="find ALL lines in the file for the word1 AND word2")
    parser.add_option("-b", "--all", type="string", dest="search_not", help="search and find the lines that contain words1 not word2")
    parser.add_option("-c", "--all", type="string", dest="search_start", help="search and find a line that starts with word1 or word2")
    parser.add_option("-d", "--all", type="string", dest="search_then", help="search and find a line that has word1 followed by word2")
    parser.add_option("-f", "--file", type="string", dest="filename", help="file name"

    if ( options.filename is None) and ( (options.search_and is None) or  (options.search_not is None) or (options.search_start is None) or (options.search_then is None)):

After the code is run by: python script.py -a hi bye

I get invalid syntax with the if statement.

0

5 Answers 5

4

The line before the if statement

parser.add_option("-f", "--file", type="string", dest="filename", help="file name"

does not have a closing ).

Python interpreter usually complains about such syntax errors in next line.So, if you get a syntax error at any line, it is advisable to check the preceding line as well

Sign up to request clarification or add additional context in comments.

Comments

1

This is equivalent to what you're trying to do, and may be slightly more readable

if options.filename is None and None in [options.search_and, options.search_not, options.search_start, options.search_then]:
  parser.error(usage)
  sys.exit()

Comments

0

You missed a ) before the :. Your editor should be catching these sort of mistakes.

6 Comments

That's because you did the same sort of error (unbalanced brackets) elsewhere.
Vazques-Abrams, I'll add what i have so far for the code. at edit#2 of the original post, give me a minute.
I just don't see it. I'm constantly rewriting it but its just not working lol.
@ino: now you missed a ) before if
Still missing a ) at the end; should be: `if options.filename is None and ((options.search_and is None) or (options.search_not is None) or (options.search_start is None) or (options.search_then is None)):
|
0

if options.filename is None and ((options.search_and is None) or (options.search_not is None) or (options.search_start is None) or (options.search_then is None))

1 Comment

Well then why "@Furbeenator: Yes, that's what I wrote.?"
0

In your original statement, "( options.filename is None)" you should remove the last ")" make replace that code with:

 (options.filename is None

That should fix your syntax error.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.