ahmetbulut
ahmetbulut

Reputation: 61

Python nested loops do not continue with the subprocess

I tried to run below code but it just loops in the very inner one and then stops, I do not understand why it doesn't continue to the outer loops?

#!/usr/bin/python3                                                                                                                                                                               

import os
import sys
import subprocess

p1 = subprocess.Popen([find...],stdout=subprocess.PIPE)
for i in range(...):
        p2 = subprocess.Popen([grep...],stdin=p1.stdout,stdout=subprocess.PIPE)
        for e in range(...):
               p3=subprocess.Popen([grep...],stdin=p2.stdout,stdout=subprocess.PIPE)
               for file in p3.stdout:
                        print(str(i)+str(e)+str(file))

Upvotes: 1

Views: 364

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

the concept is wrong

p1 = subprocess.Popen([find...],stdout=subprocess.PIPE)
for i in range(...):
        p2 = subprocess.Popen([grep...],stdin=p1.stdout,stdout=subprocess.PIPE)

when running this code (first iteration), p1.stdout is consumed fully by p2. So next spawns of p2 get an empty output.

(same goes for the inner loop)

I would write that in pure python.

First loop to match the files you want to grep on:

files_to_process = []
for root,dirs,files in os.walk(start_directory):
    files_to_process += [f for f in files if <some condition> ]

Once you have the list of files, you can open them in a lot of loops and search their content. I would advise to avoid grep and do something like:

with open(filename) as f:
    contents = f.read()
    results = re.search(regexp,contents)

(or line by line)

Upvotes: 1

Related Questions