请教python匹配
比如我想看gcc版本是否是4.6,我的想法时执行gcc -v,然后看4.6是否在gcc -v的结果中,应该怎么写?
[解决办法]
zhu@ubuntu:~/python$ python gcc_version.py
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
1
zhu@ubuntu:~/python$ cat gcc_version.py
#!/usr/bin/env python
import re
import subprocess
def check_version(ver):
vers = 'version %s' % ver
P = subprocess.Popen(('gcc', '-v'), stderr=subprocess.PIPE)
for line in P.stderr:
if re.search(vers, line):
print line,
return 1
return 0
print check_version('4.6')