Это пример того, как запустить процесс, вывод которого передаётся на вход другого подпроцесса, вывод которого передаётся в Python.
В этом примере процесс будет использовать xz для потоковой распаковки данных из input.txt.xz, которые в свою очередь передаются в sha512sum для вычисления SHA512-контрольной суммы распакованных входных данных.
Эквивалентная команда оболочки была бы xz --decompress --stdout input.txt.xz | sha512sum.
Полный исходный код
pipe_example.py
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 = subprocess.run(['sha512sum'], stdin=decompress_process.stdout, stdout=subprocess.PIPE, text=True)
decompress_process.stdout.close()
decompress_process.wait()
# Extract the SHA512 checksum from the command output
sha512sum = checksum_process.stdout.split()[0]
print(sha512sum)
Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow