Created
June 20, 2018 00:29
-
-
Save abhi1010/3509b1fd39c13198a1e3df110f2606f0 to your computer and use it in GitHub Desktop.
run bash commands in python using Popen
This file contains hidden or 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
def _run_cmd_with_prints(cmd): | |
proc = subprocess.Popen( | |
cmd, | |
bufsize=1, | |
shell=True, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
for line in proc.stdout.readline(): | |
print('output = {}'.format(line)) | |
def run_command(command): | |
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) | |
while True: | |
output = process.stdout.readline() | |
if output == '' and process.poll() is not None: | |
break | |
if output: | |
print(output.strip()) | |
rc = process.poll() | |
return rc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment