How to Compare Output from Command in Conditional Statement

I am trying to compare the output from a command in Python on my Raspberry Pi.

Here is what I have:

import commands
cmd = 'gpio read 1'
output =  commands.getoutput(cmd)
print output

The code is reading a pin on the raspberry pi to see if it is open or closed. The value will either be 0 or 1. I want to compare the output and print a message. I would like the code to look like this.


import commands
cmd = 'gpio read 1'
output =  commands.getoutput(cmd)

if output == 1:
      print ('The door is open')
else:
      print('The door is open')

When I write it like that I get a syntax error.

Please help. Thanks!
dynamicOneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

peprCommented:
The command.getoutput(cmd) returns string, not integer. Probably the easy fix is if output == '1':.

When debugging, you can print type(output) or print repr(output) to learn what the returned output really is.

Anyway, your code seems syntactically OK. Can you post the full error message?

It seems that you use Python 2.x. The print commands at the end then should not use parentheses. The else branch prints the same message as the other branch (i.e. you would not see the difference).

What version of Python do you use? The commands module is deprecated and should be replaced by subprocess.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MikeOM_DBACommented:
Does not matter because your code is useless. It will return the same response regardless of the output value.
0
peprCommented:
@Mike: Do you mean that gpio read 1 is useless? I do not know. All I know about Raspbery Pi is that it exists, and how it looks. ;)
0
MikeOM_DBACommented:
Look closely and let me know if this will produce any useful result:
if output == 1:
      print ('The door is open')
else:
      print('The door is open')

Open in new window

0
peprCommented:
@Mike: I have considered the code a prototype. You can see that I have descibed the problem in the next-to-last paragraph.
0
MikeOM_DBACommented:
Like we say here: "I was just pulling your leg" (joking, sarcastic, facetious,...)
Trying to make your day a happy day.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.