Python subprocess: How to pipe one process output into another process input

This is an example of how to start a process whose output is piped into another subprocess input, whose output is passed to Python.

In this example, the process will use xz to stream-decompress data from input.txt.xz, which is in turn passed to sha512sum to compute the SHA512 checksum of the uncompressed input data.

An equivalent shell command would be xz --decompress --stdout input.txt.xz | sha512sum.

Full source code

import subprocess

file_path = "input.txt.xz"

# Decompress the file on the fly and compute the SHA512 checksum
decompress_process = subprocess.Popen(['xz', '--decompress', '--stdout', file_path], stdout=subprocess.PIPE)
checksum_process =['sha512sum'], stdin=decompress_process.stdout, stdout=subprocess.PIPE, text=True)

# Extract the SHA512 checksum from the command output
sha512sum = checksum_process.stdout.split()[0]