1

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

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Great, that works perfectly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.