子进程:FileNotFound [英] subprocess : FileNotFound

查看:72
本文介绍了子进程:FileNotFound的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释此错误:

 >> def j():
...导入子进程
... print(subprocess.Popen(['command','-v','nmcli'],stdout = subprocess.PIPE,stderr = subprocess.PIPE))
...
>> j()
追溯(最近一次调用):
文件< stdin>,< module>中的第1行。
文件< stdin>,j中的第3行
文件 /usr/lib/python3.4/subprocess.py,线859,在__init__
restore_signals,start_new_session中)
文件 /usr/lib/python3.4/subprocess.py,行1457,在_execute_child
中引发child_exception_type(errno_num,err_msg)
FileNotFoundError:[Errno 2]没有此类文件或目录:命令

我尝试将字符串作为列表使用,它不会改变任何内容。 / p>

谢谢

解决方案


FileNotFoundError :[Errno 2]没有这样的文件或目录:'command'


command 是内置的shell。 subprocess.Popen 默认情况下不会运行外壳程序。



要运行外壳程序,请传递 shell = True

 >>导入子流程
>> subprocess.check_output('command -v python',shell = True)
b'/ usr / bin / python\n'

要找到可执行文件的完整路径,可以改为使用 shutil.which()

 >>>导入关闭项
>> shutil.which(‘python’)
’/ usr / bin / python’


Could someone explain this error to me :

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

I tried as a string as a list, it does not change anything.

Thank you,

解决方案

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

command is a shell builtin. subprocess.Popen does NOT run the shell by default.

To run the shell, pass shell=True:

>>> import subprocess
>>> subprocess.check_output('command -v python', shell=True)
b'/usr/bin/python\n'

To find the full path to an executable, you could use shutil.which() instead:

>>> import shutil
>>> shutil.which('python')
'/usr/bin/python'

这篇关于子进程:FileNotFound的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆