Created
February 27, 2019 16:58
-
-
Save takp/adf59663bf4c49a4af9e4b60df2a7591 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import subprocess | |
def execute_subprocess(command): | |
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
while True: | |
line = process.stdout.readline() | |
if line: | |
yield line | |
if not line and process.poll() is not None: | |
break | |
if __name__ == '__main__': | |
command = 'ls -l' | |
prefix = 'INFO' | |
for line in execute_subprocess(command=command): | |
log_output = "[{}] {}".format(prefix, line.decode('utf8')) | |
sys.stdout.write(log_output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the example code to execute
ls -l
command withsubprocess
.Sample output that adds
[INFO]
to the stdout: