我是Python的初学者,一直在尝试调用命令行应用程序,但失败了:
>>> import subprocess as s
>>> s.call("gpio -g read 17")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/subprocess.py", line 470, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.6/subprocess.py", line 623, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1141, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
但是如果我添加shell=True
,它就可以开始工作了。有人能解释一下为什么吗?
>>> import subprocess as s
>>> s.call("gpio -g read 17", shell=True)
>>> 0
shell = True
参数时,此命令将发送到一个 shell 中,然后由 shell 进行参数分割。 - mgilson