with open('output.bam', 'wb', 0) as output_file:
    command3 = Popen('samtools view -bS -'.split(), stdin=PIPE, stdout=
        output_file)
command1 = Popen('samtools view -HS header.sam'.split(), stdout=command3.stdin)
How to join the stdout of two subprocesses and pipe to stdin of new subprocess in python