我正在尝试从pir传感器获取信号以将其传输到Web服务。当我运行此代码时
if Current_State==1 and Previous_State==0:
# PIR is triggered
output = subprocess.check_output(["Current_State==1 and enter code herePrevious_State==0","18"]);
print " Motion detected!"
# Tell the Pi to run our speech script and speak the words
# motion dtected! - anything after the .sh will be read out.
enter code here` matches = re.search("Current_State==1 and Previous_State==0", output)
move = int(matches.group(1))
resultm = client.service.retrieveMove(move)
我收到了这个错误
**Traceback (most recent call last):
File "pir_5.py", line 48, in <module>
output = subprocess.check_output(["Current_State==1 and Previous_State==0", "18"]);
File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory****
答案 0 :(得分:6)
subprocess.checkoutput()
期望运行命令以便它可以捕获输出:
subprocess.check_output(["echo", "Hello World!"])
按预期方式为您提供字符串'Hello World!\n'
。
你给出的命令是:
["Current_State==1 and enter code herePrevious_State==0","18"]
这不太可能有效。
你需要实际弄清楚你想做什么(从问题中不清楚)然后根据它构建命令。例如,如果你想以某种方式记录(使用名为logMe
的程序),你会这样做:
output = subprocess.check_output(["logMe","CurrState=1 and PrevState=0","18"]);
答案 1 :(得分:0)
您需要在列表的第一个参数中为subprocess.check_output提供一个shell命令。然后是第2个,第3个,无论是第一个,第二个,无论shell命令的参数是什么。
Current_State == 1并在此处输入代码Previous_State == 0不是shell命令。
/ bin / true是。
pwd是。echo foo是。
ls / tmp is。