Syntax Problem

I have a Python script I am trying to run.

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

if commands.getoutput(cmd) == 0:
   print ('The door is closed.')

When I run it, nothing happens. It just goes to the next line instead of printing.

Any idea what I am doing wrong?
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.

TommySzalapskiCommented:
What are you trying to get it to do? commands.getoutput(cmd) is returning a non-zero value. What are you wanting it to return?
0
dynamicOneAuthor Commented:
This is on a raspberry pi. It should return a value of 1 or 0.
0
TommySzalapskiCommented:
So it is returning 1.
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

dynamicOneAuthor Commented:
Yes, it will return 1 when the door is open. Am I writing the condition statement wrong?
0
TommySzalapskiCommented:
If it is supposed to return 1 when the door is open and 0 when the door is closed, then your condition is fine.

Whatever is happening, it is returning 1 and that's why you don't see any output.
You could try something like this to get a better idea of what is going on

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

if output == 0:
   print ('The door is closed.')
elif output == 1:
   print ('The door is open.')
else:
   print ('Unexpected value %s' % output)

Open in new window


What happens when you just type gpio read 1 at the console?
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
dynamicOneAuthor Commented:
When type gpio read 1 at the console it outputs 0 if the door is closed and 1 if it is opened
0
dynamicOneAuthor Commented:
I found a script and was able to modify it to get what I want, is it okay with you if I post it here with my question or should I close this out and open another question?
0
TommySzalapskiCommented:
Technically, you should start a new question if it is a different issue. That's a better way to get more eyes on the issue anyway.
0
TommySzalapskiCommented:
I see the actual problem now that I saw the other script. You should have been checking if it was == to "0" instead of 0. But I think you see that now too.
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.