>>> from subprocess import Popen, PIPE
>>>
>>>
>>> def get_stderr(cmd):
... return Popen(cmd, shell=True, stderr=PIPE).communicate()[1]
...
>>>
>>> for c in ['garbage_cmd1', 'grep', 'which', 'garbage_cmd2']:
... if 'not found' in get_stderr(c).decode():
... print('%s is not valid' % c)
... else:
... print('%s is valid' % c)
...
garbage_cmd1 is not valid
grep is valid
which is valid
garbage_cmd2 is not valid
如果已经有命令列表,可以执行以下操作:
如果您在Linux机器上,也可以运行
compgen -c >> cmds.txt
相关问题 更多 >
编程相关推荐