子进程:FileNotFound
subprocess: FileNotFound
谁能给我解释一下这个错误:
>>> def j():
... import subprocess
... print(subprocess.Popen(['command', '-v', 'nmcli'], stdout=subprocess.PIPE, stderr=subprocess.PIPE))
...
>>> j()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in j
File "/usr/lib/python3.4/subprocess.py", line 859, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.4/subprocess.py", line 1457, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'command'
我尝试将字符串作为列表,它没有改变任何东西。
FileNotFoundError: [Errno 2] No such file or directory: 'command'
command
是一个 shell 内置函数。默认情况下 subprocess.Popen
不 运行 shell。
给运行 shell,传shell=True
:
>>> import subprocess
>>> subprocess.check_output('command -v python', shell=True)
b'/usr/bin/python\n'
要找到可执行文件的完整路径,您可以 use shutil.which()
instead:
>>> import shutil
>>> shutil.which('python')
'/usr/bin/python'
谁能给我解释一下这个错误:
>>> def j():
... import subprocess
... print(subprocess.Popen(['command', '-v', 'nmcli'], stdout=subprocess.PIPE, stderr=subprocess.PIPE))
...
>>> j()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in j
File "/usr/lib/python3.4/subprocess.py", line 859, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.4/subprocess.py", line 1457, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'command'
我尝试将字符串作为列表,它没有改变任何东西。
FileNotFoundError: [Errno 2] No such file or directory: 'command'
command
是一个 shell 内置函数。默认情况下 subprocess.Popen
不 运行 shell。
给运行 shell,传shell=True
:
>>> import subprocess
>>> subprocess.check_output('command -v python', shell=True)
b'/usr/bin/python\n'
要找到可执行文件的完整路径,您可以 use shutil.which()
instead:
>>> import shutil
>>> shutil.which('python')
'/usr/bin/python'