我正在尝试列出我的linux (Lubuntu)机器上可用的每个命令。我想在Python中进一步处理这个列表。通常,为了在控制台中列出命令,我会编写"compgen -c“,它会将结果打印到stdout。
我想使用Python子进程库来执行该命令,但它给了我一个错误,我不知道为什么。
代码如下:
#!/usr/bin/python
import subprocess
#get list of available linux commands
l_commands = subprocess.Popen(['compgen', '-c'])
print l_commands下面是我得到的错误:
Traceback (most recent call last):
File "commands.py", line 6, in <module>
l_commands = subprocess.Popen(['compgen', '-c'])
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory我被卡住了。你们能帮我个忙吗?如何使用子进程执行compgen命令?
发布于 2014-05-09 03:49:20
compgen is a builtin bash command,在shell中运行它:
from subprocess import check_output
output = check_output('compgen -c', shell=True, executable='/bin/bash')
commands = output.splitlines()你也可以把它写成:
output = check_output(['/bin/bash', '-c', 'compgen -c'])但它将essential部分(compgen)放在最后,所以我更喜欢第一个变体。
发布于 2014-05-09 03:20:57
我不确定compgen是什么,但这个路径必须是绝对的。当我使用子进程时,我拼写出确切的页面/absolute/path/to/compgen
https://stackoverflow.com/questions/23550650
复制相似问题