How to store output of os.system() in a variable

os.system, python

Solution

The `os.system` return the exit code of the command.

To capture the output of the command, you can use subprocess.check_output

output = subprocess.check_output('users', shell=True)

Problem

I wrote a small code: ``` import os os.system('users') os.system('w') ``` This prints ``` ubuntu 09:27:25 up 9 days, 21:23, 1 user, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT ubuntu pts/0 42.99.164.66 09:06 5.00s 0.10s 0.00s sh -c w ``` But when i try : ``` import os from pyslack import SlackClient user_name = os.system('users') login_details = os.system('w') print user_name print login_details ``` It has the following output: ``` ubuntu 09:28:32 up 9 days, 21:24, 1 user, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT ubuntu pts/0 42.99.164.66 09:06 0.00s 0.11s 0.00s w 0 0 ``` Now i am not sure why i am not able to store the result in the varible , i.e why is it printing 0 ? And what should be the correct way to get rid of it?

Original source

Related problems