Skip to content

Instantly share code, notes, and snippets.

@samuell
Last active December 27, 2015 09:49
Show Gist options
  • Select an option

  • Save samuell/7306958 to your computer and use it in GitHub Desktop.

Select an option

Save samuell/7306958 to your computer and use it in GitHub Desktop.
A convenience method for executing external commands and getting the output, in python
import subprocess as sub
def exec_cmd(commandstr):
proc = sub.Popen(commandstr, stdout=sub.PIPE, shell=True)
return proc.communicate()[0].strip()
@samuell

samuell commented Nov 4, 2013

Copy link
Copy Markdown
Author

Used like this, for executing jar files:

output = exec_cmd("java -jar SomeJarFile.jar")

@samuell

samuell commented Nov 4, 2013

Copy link
Copy Markdown
Author

Learned, in this thread [1], that I can do:

import subprocess as sub

output = sub.check_output("java -jar SomeJarFile.jar")

... or (if using http://amoffat.github.io/sh/):

import sh
output = sh.java("-jar", "SomeJarFile.jar")

Nice!

[1] https://plus.google.com/u/0/106548631604770110294/posts/Z87xvsNcGWY

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