Skip to content
Advertisement

subprocess popen is returning blanck output

On trying to run the grep for the output of previous command using popen returning blank without any error

proc1cmd = "grep " + fetchname1
   p1 = subprocess.Popen(['kubectl', 'get', 'abr', '-A'], stdout=subprocess.PIPE)
   p2 = subprocess.Popen(proc1cmd, shell=True, stdin=p1.stdout, stdout=subprocess.PIPE, 
   stderr=subprocess.PIPE)
   p1.stdout.close() 
   stdout_list = p2.communicate()[0]
   stdout_list = stdout_list.decode()
   print(p2.stdout)
   print(p2.communicate())

output i got:

<_io.BufferedReader name=7>
   (b'', b'')

Advertisement

Answer

You don’t need to concoct a pipeline of kubectl + grep here.

kubectl_output = subprocess.check_output(
    ["kubectl", "get", "abr", "-A"], encoding="utf-8"
)
matching_lines = [
    line for line in kubectl_output.splitlines() if fetchname1 in line
]
print(matching_lines)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement