crusadecoder crusadecoder - 1 year ago 90
Python Question

python if statement printing incorrect condition

Can someone please assist me with what i am doing wrong here?

[email protected]:$ cat Names
This is Paul


This is my python script

from subprocess import *
test = check_output("grep Paul Names", stderr=STDOUT, shell=True )

if test is True :
print ("Correct")
else:
print("Incorrect")


Result

[email protected]:$ python find.py
Incorrect


This should print correct.

Answer Source

Using is is an identity operator, so it is not performing the comparison you think it is. It compares to see if you are pointing to the same object.

Your check should simply be:

if test:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download