os.system and command output

A

A.M

Hi,



How can I run an OS command and have the command's output (to stdout) in my
string variable?



For example in windows, this command should return a list of files within
directory:



os.system("DIR")



I am looking for an alternative function that returns the DIR command output
in a string



Thanks,

Alan
 
S

Steve Holden

A.M said:
Hi,



How can I run an OS command and have the command's output (to stdout) in my
string variable?



For example in windows, this command should return a list of files within
directory:



os.system("DIR")



I am looking for an alternative function that returns the DIR command output
in a string
Try:

s = os.popen("DIR").read()

regards
Steve
 
F

Fredrik Lundh

A.M said:
Is there anyway I can get the exit code (what os.system returns) from
os.popen?

you'd rather kill yourself than read the documentation, right?

http://www.python.org/doc/lib/os-newstreams.html#os-newstreams

"The exit status of the command (encoded in the format specified
for wait()) is available as the return value of the close() method
of the file object, except that when the exit status is zero
(termination without errors), None is returned."

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top