Assign output of os.system to a variable and prevent it from being displayed on the screen [duplicate]
Asked Answered
I

8

435

I want to assign the output of a command I run using os.system to a variable and prevent it from being output to the screen. But, in the below code ,the output is sent to the screen and the value printed for var is 0, which I guess signifies whether the command ran successfully or not. Is there any way to assign the command output to the variable and also stop it from being displayed on the screen?

var = os.system("cat /etc/services")
print var #Prints 0
Ionia answered 17/8, 2010 at 15:3 Comment(8)
Don't use os.system (nor os.popen, per the answer you accepted): use subprocess.Popen, it's way better!Eve
@AlexMartelli, one can't use a complex commands (e.g. piped) in subprocess.Popen(), but with os.system one canKibosh
@vak, of course you can use pipes &c w/subprocess.Popen -- just add shell=True!Eve
@AlexMartelli shell=True is (generally) a very bad idea! You have to be very sure of what you're executing :)Carousel
@AlexMartelli it's not useful to say that something is "way better" without saying why.Glyceric
@user650261, as is pretty obvious as soon as you look into os.system, os.popen, and subprocess.Popen, the latter gives you far more fine-grained control. That clearly will be "way better" if you need something different than the simpler approaches' behavior, as the OP evidently does.Eve
@IgnacioFernández, os.open and os.popen require exactly the same degree of certainty about what you're executing as subprocess.Popen with shell=True, so that's absolutely no reason to avoid the latter.Eve
@AlexMartelli this is Stack Overflow, where the goal is to provide that type of information to people asking questions rather than telling them to rtm. Even now, it is not clear what fine-grained control is more useful for OP's case.Glyceric
O
652

From this question which I asked a long time ago, what you may want to use is popen:

os.popen('cat /etc/services').read()

From the docs for Python 3.6,

This is implemented using subprocess.Popen; see that class’s documentation for more powerful ways to manage and communicate with subprocesses.


Here's the corresponding code for subprocess:

import subprocess

proc = subprocess.Popen(["cat", "/etc/services"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print("program output:", out)
Offshoot answered 17/8, 2010 at 15:6 Comment(14)
Note that Walter's subprocess.check_output solution is closer to the Pythonic one-liner it seems you're looking for, as long as you don't care about stderr.Minestrone
@ChrisBunch Why is suprocess a better solution than os.popen?Rosenberg
You should (almost) never use shell=True. See docs.python.org/3/library/…Glochidium
what is the difference between os.Popen and subprocess.popen?Yogi
Good answer! Question though: Why in this example is stdout=PIPE specified, but not stderr=PIPE too? In example I see: ((out, err) = proc.communicate() to capture both stdout and stderr, but if you don't use stderr=PIPE, will err ever get populated? Intentional or typo?Westbrooks
Is it a good idea to close this open pipe at any point? Or should we just leave it because it's not taking up an resources?Trachytic
I keep coming across this and every time I wonder - why is it better to have three lines of complicated code rather than one line of obvious code?Petronel
Not only should you (almost) never use shell=True, but it's also incorrect in this case. shell=True makes the shell the child process rather than cat, so out and err are the stdout/stderr of the shell process rather than of the cat processExtrusion
python retains the '\n' at the end so you probably want to strip that if you want exact compatability.Fullbodied
How can we avoid to remove '\n'. I am facing this prob nowThymic
catching into variable seems to be more convenient in understanding in first glance like, output = os.popen('ls').read()Bearer
@Petronel is it better to try to slice a stick of butter with the flat side of a knife or the edge? Is it better to try to squish a stick of butter with the flat side of a knife, or the edge? Is it better to write two simple wrappers around a single, complex implementation of "knife", or is it better to create multiple implementations of the complex system, duplicating code and errors, and create a 1:1 mapping between simple interfaces at every level? -- This last question definitely gets a "no" -- the knife is an efficient construct, and the range of interactions is complex to specify.Purgatory
Should add .rstrip() at the end to remove the extra line added by popenAdieu
If you want to execute a shell script with popen(), you must run the 'sh' command. E.g. subprocess.Popen(["/bin/sh", "/home/user/myscript", .....]Bydgoszcz
C
238

You might also want to look at the subprocess module, which was built to replace the whole family of Python popen-type calls.

import subprocess
output = subprocess.check_output("cat /etc/services", shell=True)

The advantage it has is that there is a ton of flexibility with how you invoke commands, where the standard in/out/error streams are connected, etc.

Catha answered 17/8, 2010 at 15:29 Comment(7)
note, check_output was added with python 2.7 docs.python.org/2.7/library/…Cantabrigian
Add stderr=subprocess.STDOUT to capture standard errors as wellMixedup
i used this method with systeminfo command. Output was something nasty like this b'\r\nHost Name: DIMUTH-LAPTOP\r\nOS Name: Microsoft Windows 10 Pro\r\nOS Version: 10.0.10240 N/A Build 10240\r\nOS Manufacturer: Microsoft CorporationNerynesbit
how can i print in line by line . because above output is a messNerynesbit
@Nerynesbit found a solution?Moiety
@Moiety Not yet !!Nerynesbit
@ghost21blade, you might want to look into this: output.decode("utf-8").split("\n") or whatever you´d like to do with it. You must convert it into a string first!Hollishollister
E
48

The commands module is a reasonably high-level way to do this:

import commands
status, output = commands.getstatusoutput("cat /etc/services")

status is 0, output is the contents of /etc/services.

Eggnog answered 17/8, 2010 at 15:22 Comment(3)
Quoting from the documentation of the commands module: "Deprecated since version 2.6: The commands module has been removed in Python 3. Use the subprocess module instead.".Autograft
sure it's outdated but sometimes you just want to get something done quickly and easily in a few lines of code.Megaron
@advocate check out the check_output command of subprocess. It's quick, easy, and won't depreciate soon!Cortez
F
43

For python 3.5+ it is recommended that you use the run function from the subprocess module. This returns a CompletedProcess object, from which you can easily obtain the output as well as return code. Since you are only interested in the output, you can write a utility wrapper like this.

from subprocess import PIPE, run

def out(command):
    result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
    return result.stdout

my_output = out("echo hello world")
# Or
my_output = out(["echo", "hello world"])
Fiddlefaddle answered 17/3, 2016 at 10:45 Comment(2)
Dont forget "capture_output=True" option for run()Mitosis
Using: result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True, capture_output=True) with capture_output injected returns: ValueError: stdout and stderr arguments may not be used with capture_output.Steinke
M
36

I know this has already been answered, but I wanted to share a potentially better looking way to call Popen via the use of from x import x and functions:

from subprocess import PIPE, Popen


def cmdline(command):
    process = Popen(
        args=command,
        stdout=PIPE,
        shell=True
    )
    return process.communicate()[0]

print cmdline("cat /etc/services")
print cmdline('ls')
print cmdline('rpm -qa | grep "php"')
print cmdline('nslookup google.com')
Maurilia answered 22/5, 2014 at 2:6 Comment(2)
3 years later, this is what worked for me. I threw this in a separate file called cmd.py, and then in my main file I wrote from cmd import cmdline and used it as needed.Dy
I was getting the same issue with os.system returning 0, but I tried this method and it works great! And as a bonus it looks nice and tidy :) Thanks!Bolshevism
B
6

I do it with os.system temp file:

import tempfile, os

def readcmd(cmd):
    ftmp = tempfile.NamedTemporaryFile(suffix='.out', prefix='tmp', delete=False)
    fpath = ftmp.name
    if os.name=="nt":
        fpath = fpath.replace("/","\\") # forwin
    ftmp.close()
    os.system(cmd + " > " + fpath)
    data = ""
    with open(fpath, 'r') as file:
        data = file.read()
        file.close()
    os.remove(fpath)
    return data
Buckskins answered 7/9, 2015 at 8:14 Comment(0)
S
3

Python 2.6 and 3 specifically say to avoid using PIPE for stdout and stderr.

The correct way is

import subprocess

# must create a file object to store the output. Here we are getting
# the ssid we are connected to
outfile = open('/tmp/ssid', 'w');
status = subprocess.Popen(["iwgetid"], bufsize=0, stdout=outfile)
outfile.close()

# now operate on the file
Shock answered 1/7, 2016 at 19:25 Comment(0)
P
-1
from os import system, remove
from uuid import uuid4

def bash_(shell_command: str) -> tuple:
    """

    :param shell_command: your shell command
    :return: ( 1 | 0, stdout)
    """

    logfile: str = '/tmp/%s' % uuid4().hex
    err: int = system('%s &> %s' % (shell_command, logfile))
    out: str = open(logfile, 'r').read()
    remove(logfile)
    return err, out

# Example: 
print(bash_('cat /usr/bin/vi | wc -l'))
>>> (0, '3296\n')```
Polyphemus answered 1/7, 2020 at 3:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.