Cat、grep和重定向到python

Cat、grep和重定向到python,python,Python,这是我尝试过的…不起作用 我能想到的最简单的解决方案: with open("filename") as origin_file: for line in origin_file: line = re.findall(r"something",line) f=open('file2.txt','w') subprocess.call('line',stdout=f) f.close() 为什么不起作用?可能是重复的 with open(

这是我尝试过的…不起作用

我能想到的最简单的解决方案:

with open("filename") as origin_file:
    for line in origin_file:
        line = re.findall(r"something",line)

    f=open('file2.txt','w')
    subprocess.call('line',stdout=f)    
    f.close()

为什么不起作用?可能是重复的
with open("filename") as origin_file:
    for line in origin_file:
        line = re.findall(r"something",line)

    f=open('file2.txt','w')
    subprocess.call('line',stdout=f)    
    f.close()
with open('filename', 'r') as in_f, open('file2.txt', 'a') as out_f:
    for line in in_f:
        if 'something' in line:
            out_f.write(line)