python调用grep返回结果

下面的函数封装了系统的grep功能,传递文件名和参数返回grep的结果
import subprocess

def grep(filename, arg):
    process = subprocess.Popen(['grep', '-n', arg, filename], stdout=subprocess.PIPE)
    stdout, stderr = process.communicate()
    return stdout, stderr

编程技巧