How can I use input redirection <() with python's subprocess.Popen?
For example, say I have:
import subprocess
class Test():
def __init__(self):
self.proc = subprocess.Popen(["sort file1.txt file2.txt)"],
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
def __iter__(self):
return self
def __next__(self):
while True:
line = self.proc.stdout.readline()
if not line:
raise StopIteration
return line.strip().decode('utf-8')
t = Test()
for line in t:
print(line)
The above works perfectly fine, but really I need the command to do something like:
sort <(python file1.txt) <(python file2.txt)
That doesnt seem to run anything though, even this doesnt work
sort <(cat file1.txt) <(cat file2.txt)
How can I get this to work with python's subprocess, and iterate through the results line by line
asked Dec 9, 2021 at 19:22
user1179317
2,9634 gold badges43 silver badges81 bronze badges
1 Answer 1
You should tell subprocess.Popen() to use /bin/bash, which supports the <(..) syntax, instead of the default /bin/sh, which doesn't:
def __init__(self):
self.proc = subprocess.Popen(["sort <(cat file1.txt) <(cat file2.txt)"],
shell=True,
executable="/bin/bash",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
def __iter__(self):
return self
def __next__(self):
while True:
line = self.proc.stdout.readline()
if not line:
raise StopIteration
return line.strip().decode('utf-8')
t = Test()
for line in t:
print(line)
answered Dec 9, 2021 at 21:03
Michael Veksler
8,5101 gold badge24 silver badges37 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
user1179317
Great, that works perfectly
lang-py