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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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)!)
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)!)
ASKER
i will answer myself
input, output = os.popen2("ls", mode=t)
ASKER
I am curious about the read() function
Wich others are available?