Skip to content

Instantly share code, notes, and snippets.

@voodoonofx
Last active September 20, 2022 23:05
Show Gist options
  • Save voodoonofx/8e5bcae8e0b0c5741148 to your computer and use it in GitHub Desktop.
Save voodoonofx/8e5bcae8e0b0c5741148 to your computer and use it in GitHub Desktop.
subprocess.Popen Example
import subprocess
cmd = ['/full/path/to/python', '/full/path/to/your/process']
print('running command: "{0}"'.format(cmd)) # output the command.
# Here, we join the STDERR of the application with the STDOUT of the application.
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
outLines = [] # We use this just in case you want to see the output of the program later. It's not required.
for line in iter(process.stdout.readline, ''):
print line.rstrip()
outLines.append(line) # Again, only if you need it
returncode = process.wait() # Wait for the underlying program to finish normally.
print('sub-process exited with code: {0}'.format(returncode))
@voodoonofx
Copy link
Author

voodoonofx commented Sep 20, 2022

In python3, update line 9 to the following:

for line in iter(process.stdout.readline, b''):
    print(line.rstrip())
    outLines.append(line)  # Again, only if you need it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment