Avatar of dynamicOne
dynamicOne asked on

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?
PythonProgramming Languages-OtherProgramming Theory

Avatar of undefined
Last Comment
TommySzalapski

8/22/2022 - Mon
TommySzalapski

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?
ASKER
dynamicOne

This is on a raspberry pi. It should return a value of 1 or 0.
TommySzalapski

So it is returning 1.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER
dynamicOne

Yes, it will return 1 when the door is open. Am I writing the condition statement wrong?
ASKER CERTIFIED SOLUTION
TommySzalapski

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
dynamicOne

When type gpio read 1 at the console it outputs 0 if the door is closed and 1 if it is opened
ASKER
dynamicOne

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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
TommySzalapski

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.
TommySzalapski

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.