1

I am having trouble with what is presumably a simple if statement. I am trying to pass through the type of VM file format to use. Even if I put VMDK or VHD, it still comes back with VMDK is an invalid type or VHD is an invalid type.

import sys
vmtype = sys.argv[3]
vmtype = str(vmtype).strip().upper()
## File format check
if vmtype is not ("VHD") or ("VMDK"):
    print vmtype + " is an invalid type"
    sys.exit()

I have tried the if statement with != and not putting the parameters in parentheses. I have searched the web for a while and have tried what I have found and am still running into the same issue.

FYI I am running Python 2.6.5

1 Answer 1

9

Try:

if vmtype not in ("VHD", "VMDK"):

Your current code parses as:

if (vmtype is not ("VHD")) or ("VMDK"):

Which is obviously wrong. Since ("VMDK") is a truthy value, the whole statement always be true. Therefore the if statement will always execute.


Note that even if you tried

if vmtype is not "VHD":

It would not work, because is tests identity, not value. You would use:

if vmtype != "VHD":
Sign up to request clarification or add additional context in comments.

2 Comments

Other bug: is tests object identity instead of value equality. The not in solution happens to solve that problem too, but I'm not sure whether you noticed.
Doorknob, the not in works great. Thank you for the insight. I am still learning Python so I am prone to rookie mistakes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.