4 Copy edited (e.g. ref. <http://en.wikipedia.org/wiki/Python_%28programming_language%29>). (its = possessive, it's = "it is" or "it has". See for example <http://www.wikihow.com/Use-Its-and-It%27s>.)
source | link

Running shell command from python Python and capturing the output

I want to write a function that will execute a shell command and return it's its output as a string, no matter, is it an error or success message. I just want to get the same result that I would have gotten with the command line.

Can anybody give What would be a code example that would do such a thing?

For example:

def run_command(cmd):
    # ??????

print run_command('mysqladmin create test -uroot -pmysqladmin12')
# shouldShould output something like:
# mysqladmin: CREATE DATABASE failed; error: 'Can't create database 'test'; database exists'

Running shell command from python and capturing the output

I want to write a function that will execute a shell command and return it's output as a string, no matter, is it an error or success message. I just want to get the same result that I would have gotten with the command line.

Can anybody give a code example that would do such a thing?

For example:

def run_command(cmd):
    # ??????

print run_command('mysqladmin create test -uroot -pmysqladmin12')
# should output something like:
# mysqladmin: CREATE DATABASE failed; error: 'Can't create database 'test'; database exists'

Running shell command from Python and capturing the output

I want to write a function that will execute a shell command and return its output as a string, no matter, is it an error or success message. I just want to get the same result that I would have gotten with the command line.

What would be a code example that would do such a thing?

For example:

def run_command(cmd):
    # ??????

print run_command('mysqladmin create test -uroot -pmysqladmin12')
# Should output something like:
# mysqladmin: CREATE DATABASE failed; error: 'Can't create database 'test'; database exists'
    Question Protected by J.F. Sebastian
3 edited tags
| link
2 added 275 characters in body
source | link

I want to write a function that will execute a shell command and return it's output as a string, no matter, is it an error or success message. I just want to get the same result that I would have gotten with the command line.

Can anybody give a code example that would do such a thing?

For example:

def run_command(cmd):
    # ??????

print run_command('mysqladmin create test -uroot -pmysqladmin12')
# should output something like:
# mysqladmin: CREATE DATABASE failed; error: 'Can't create database 'test'; database exists'

I want to write a function that will execute a shell command and return it's output as a string, no matter, is it an error or success message. I just want to get the same result that I would have gotten with the command line.

Can anybody give a code example that would do such a thing?

I want to write a function that will execute a shell command and return it's output as a string, no matter, is it an error or success message. I just want to get the same result that I would have gotten with the command line.

Can anybody give a code example that would do such a thing?

For example:

def run_command(cmd):
    # ??????

print run_command('mysqladmin create test -uroot -pmysqladmin12')
# should output something like:
# mysqladmin: CREATE DATABASE failed; error: 'Can't create database 'test'; database exists'
1
source | link