Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2005-04-26
Medium Priority
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
  • 3
  • 2
LVL 14

Accepted Solution

RichieHindle earned 400 total points
ID: 13870972
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 http://docs.python.org/lib/os-newstreams.html 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

ID: 13871300
Thanks it is working,

I am curious about the read() function

Wich others are available?

Author Comment

ID: 13871482

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

ID: 13871573
RobertoDeAlba: "how should I specify the mode?"

It's a string:

import os
input, output = os.popen2("ls", "t")
lines = output.read().split('\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

ID: 13871622

i will answer myself

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…
Suggested Courses

581 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