OSError: [errno2] 没有这样的文件或目录

0 投票
2 回答
3093 浏览
提问于 2025-04-18 00:05

我正在尝试获取来自 PIR 传感器的信号,并将其传输到网络服务。当我运行这段代码时

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****

2 个回答

0

你需要在列表的第一个参数中给subprocess.check_output一个命令,这个命令是你想在命令行中执行的。然后,列表中的第二个、第三个等等的元素就是这个命令的参数。

比如说,Current_State==1和enter code herePrevious_State==0并不是一个有效的命令。

/bin/true是一个有效的命令。

pwd也是一个有效的命令。

echo foo也是一个有效的命令。

ls /tmp也是一个有效的命令。

6

subprocess.checkoutput() 是用来执行一个 命令 的,这样它就可以捕获这个命令的输出,正如 Python 文档中的经典示例所示:

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"]);

撰写回答