Question:
following some examples from https://www.programcreek.com/python/example/121/getopt.getopt where they make option comparisons with == i.e (option == “–json”), I am trying to enter the if statement. However option will not match despite being of type string and “–json”. Can anyone see what is going wrong?–json
<class ‘str’>
Answer:
You are using thelongopts
parameter incorrectly. Using an =
in the option indicates that the option takes arguments but you are not passing your data
argument to the --json
option, you are passing it to the script as a global argument (because you omitted the =
from the --json
option).Note also that you must remove the whitespace between the option and its argument. Finally, you should never use a bare except.
If you have better answer, please add a comment about this, thank you!