Link to home
Start Free TrialLog in
Avatar of RobertoDeAlba
RobertoDeAlba

asked on

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


Experts,

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
os.system()

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
ASKER CERTIFIED SOLUTION
Avatar of RichieHindle
RichieHindle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RobertoDeAlba
RobertoDeAlba

ASKER

Thanks it is working,

I am curious about the read() function

Wich others are available?


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?

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)!)


i will answer myself

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