How to store output of os.system() in a variable
Solution 1
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)
Solution 2
The value returned by the os.system is identical to the return value of the command you launched. Since most calls, like 'users' are written in C, they return 0 when the code is executed successfully ( they have a return 0;
at the end of the main()
).
If you want to save their output, you can redirect their output path (by default stdout
) to a text file, then read the text file.
user_name = os.system('users > users.txt')
login_details = os.system('w > w.txt')
with open("users.txt", "r") as f:
for line in f:
print line
with open("w.txt", "r") as f:
for line in f:
print line
os.system("rm users.txt")
os.system("rm w.txt")
I bow to the subprocess.check_output
solution
Solution 3
From the os.system(command).
os.system
just execute the command (a string) in a subshell.
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
Which means the above data is the output written to stdout by calling the Standard C function system()
not the return value.
On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.
On Windows, the return value is that returned by the system shell after running command, given by the Windows environment variable COMSPEC: on command.com systems (Windows 95, 98 and ME) this is always 0; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status of the command run; on systems using a non-native shell, consult your shell documentation.
So if the exit status is success, user_name
and login_details
will get a zero.
As a matter of fact, you can try this:;
import subprocess
user = subprocess.check_output(['users'])
details = subprocess.check_output(['w'])
print(user)
print(details)
Comments
-
Nishant Singh almost 2 years
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?
-
Nishant Singh almost 7 yearsIf i have to get the specific fields out of details , lets say FROM , i,e the IP it was logged in , how can we achieve that
-
McGrady almost 7 years@NishantSingh You need
awk
try to run this commandw | awk '{print $3}'
in terminal or you can split thelogin_details
string. -
Daniyal Warraich over 2 yearsIf the return code is not 0, then
check_output
returns an error.