How can I call an external program which is written in bash script in such a way that the output produced by that script is available in sys.stdout so that I can log the output in a file through python.
For example. I now call them through the following snippet
if os.name == 'nt':
path = module_dir_run+'/run.bat'
else:
path = module_dir_run+'/run.sh'
if os.path.isfile(path):
if (splitargs.arg):
try:
call([path, splitargs.arg])
except:
pass
else:
try:
call([path])
except:
pass
else:
print "Not found : " + path
when I store the value of sys.stdout = file(filename, "w") it stores whatever which the python outputs, but not what the script outputs.
NOTE: The script which i am trying to run is an interactive script, so after the call has ended, and the control has come back to python, how can i get all what is written in the terminal?
Any suggestions?
I always use subprocess.Popen() to run another program from inside a Python script. Example:
You can redirect the output of process to another file like this:
Redirecting stderr is also possible through ‘stderr’ parameter.
When you have the
sys.stdoutin current script redirected to write to your own file, you can do this to redirect it in your subprocess too: