How can I store the results that are on the screen after issung a command?

Posted on 2005-04-26
Last Modified: 2010-04-16


Hi, I am new in to python and I am trying to interact with the shell, under linux,

I am issuing some commands in this way:

import os

It works fine, it issues the execute file, and exexutes it.

but now I need to store the results in a variable so then I can parse the results,
sometimes it a single line sometimes it is a long listing,

How can I store the results that are on the screen after issung a command??

Best regards
Question by:RobertoDeAlba
    LVL 14

    Accepted Solution

    To capture the output, use os.popen().  This returns a file-like object that you can read() to get the output:

    import os
    output = os.popen("ls").read()
    lines = output.split('\n')
    print "There are %d files." % len(output)

    See for the documentation.  There are various flavours of popen that can let you both read and write to the child process, read its output and error output separately, etc.

    Author Comment

    Thanks it is working,

    I am curious about the read() function

    Wich others are available?

    Author Comment


    I have jsut read the docs,

    I found something that can be very useful to me   popen2

    I tried:

      import os
      output = os.popen2("ls",r).read()
      lines = output.split('\n')
      print "There are %d files." % len(output)

    but it returns "NameError: name 'r' is not defined"  ,   how should I specify the mode?

    LVL 14

    Expert Comment

    RobertoDeAlba: "how should I specify the mode?"

    It's a string:

    import os
    input, output = os.popen2("ls", "t")
    lines ='\n')
    print "There are %d files." % len(lines)

    (Note the bug in my original answer - I said len(output) when I meant len(lines)!)

    Author Comment


    i will answer myself

    input, output = os.popen2("ls", mode=t)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
    The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now