Python Subprocess Output Handling: Read and Print Stdout/Stderr
The code snippet provided seems to be incomplete and contains a syntax error. The print statements inside the if and else blocks are missing the f-string prefix 'f' to format the variables.
Here's a corrected version of the code:
result = stdout.read().decode('utf-8')
error = stderr.read().decode('utf-8')
if error:
print(f'ERROR: Failed to execute script on {node}: {error}')
else:
print(f'Result from node {node}:
{result}')
This code reads the stdout and stderr streams from a subprocess and prints the output or error message depending on whether the error variable is empty or not. The f-strings are used to format the variables node, error, and result into the print statements.
原文地址: https://www.cveoy.top/t/topic/nNz0 著作权归作者所有。请勿转载和采集!