Run python inside python and capture output

This is taken from http://techkr.blogspot.com/2007/02/python-how-to-get-output-from-ossystem.html:

How to execute python inside python and capture output:

A quick solution using popen :

import os
result=os.popen('command').read()
print result

where command is your command (expl. on windows: dir,ipconfig,...)

If you expect more than a line and want each word in a list:

import os,string
result=string.split(os.popen('dir/w').read())

Comments

Popular posts from this blog

async promises in a sync way?

Git commands cheat sheet (in progress)

Cut/Copy and paste in vi