2016-01-27 4 views
0

Может кто-нибудь объяснить эту ошибку мне:подпроцесс: 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' 

Я попытался как строку в списке, это ничего не меняет.

Спасибо,

ответ

2

FileNotFoundError: [Errno 2] No such file or directory: 'command'

command является встроенной командой оболочки. subprocess.Popen НЕ запускает оболочку по умолчанию.

Для запуска оболочки, проходят 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' 
+0

Я на самом деле пытался POPEN с оболочкой, установленной на Верно, но это вернул 2 пустые строки 'b'. И спасибо за подсказку, я никогда не думаю об этом: теперь я буду! Еще раз спасибо ! –

Смежные вопросы