Skip to content

Instantly share code, notes, and snippets.

@abhi1010
Created June 20, 2018 00:29
Show Gist options
  • Save abhi1010/3509b1fd39c13198a1e3df110f2606f0 to your computer and use it in GitHub Desktop.
Save abhi1010/3509b1fd39c13198a1e3df110f2606f0 to your computer and use it in GitHub Desktop.
run bash commands in python using Popen
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