0

The expected result in result variable is root. version: Python 3.6.7 (default, Oct 22 2018, 11:32:17)

>>> import os
>>> os.system("stat -c '%U' /tmp/test")
    root
    0
>>> result = os.system("stat -c '%U' /tmp/test")
    root
>>> print(result)
    0
Jamshy
  • 60
  • 1
  • 11
  • 1
    I think the functionality you're looking for is described here... https://stackoverflow.com/questions/4760215/running-shell-command-and-capturing-the-output – Daniel Marchand Dec 17 '18 at 11:43

3 Answers3

1

From the docs of os.system:

On Unix, the return value is the exit status of the process encoded in the format specified for wait()

Your command executes without error, so its exit status is 0, which is what system returns. If you want to get the output of the command you run, you'll need to call the command via one of the subprocess module's functions.

GPhilo
  • 18,519
  • 9
  • 63
  • 89
0

as per description at this link the os.system() returns the exit code of the command and not the actual output. Instead of this you can use subprocess module.

Andrii Rusanov
  • 4,405
  • 2
  • 34
  • 54
Amit Nanaware
  • 3,203
  • 1
  • 6
  • 19
-1

os.system is used to just get the return code, in your case try to use subprocess.check_output

from subprocess import check_output
result = check_output(["stat","-c","'%U'","/tmp/test"])
print result
root
Jamshy
  • 60
  • 1
  • 11
Shrey
  • 1,242
  • 1
  • 13
  • 27