子过程:FileNotFound [英] subprocess: FileNotFound

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

问题描述

有人可以向我解释这个错误吗?

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.

推荐答案

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

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

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

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

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

To run the shell, pass shell=True:

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

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

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天全站免登陆