python 子进程调用 OSX

1 投票
2 回答
854 浏览
提问于 2025-04-17 10:21

我正在尝试通过Python访问wifi接口:在bash中我可以使用以下命令

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/sbin/airport -I

-s这个选项也可以加上。

我在Python中尝试了以下代码:

from subprocess import call
call(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/sbin/airport', '-I'])

但是结果明显不对,我得到了这样的回复:

Traceback (most recent call last):
  File "ip3.py", line 5, in <module>
    call(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/sbin/airport', '-I'])
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/subprocess.py", line 467, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/subprocess.py", line 741, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/subprocess.py", line 1356, in _execute_child
    raise child_exception_type(errno_num, err_msg)
OSError: [Errno 2] No such file or directory: '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/sbin/airport'

如果有任何建议我都会很欢迎... 我只是想先把这个打印到屏幕上,保存成数组等等...


我现在的评分还不够高,不能回答我自己的问题,所以我在这里说一下!

其实我之前有点傻!

from subprocess import call
call(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport', '-I'])

现在可以正常工作了。只需要去掉 /usr/sbin/airport 这个部分。

2 个回答

0

试试这样做

from subprocess import call
 call(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport', '/usr/sbin/airport', '-I'])

否则,它会把 /usr/sbin/airport 当作第一个路径的一部分。

1

调用时,首先把第一个参数当作命令,后面的参数则是给这个命令的。

在你的例子中,

命令是:

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport

而这个命令的两个参数是:

  1. /usr/sbin/airport
  2. -I

所以,你需要这样调用它:

from subprocess import call
call(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport' '/usr/sbin/airport', '-I'])

撰写回答