1

I am trying to redirect stdout to a file, Where print statements are being redirected but os.system o/p is not.

From This I have tried using ">" operator but not working for me.

I don't want to use subprocess or popen,

Below is the sample code.

Can any one help?

def ExecCMS_AGT_DB(cmd):
        sys.stdout=open(cmd+'.txt','w')
        print "\ncmd $: "+cmd+" start"
        os.system(cmd+" start")
        print "\ncmd $: "+cmd+" stop"
        os.system(cmd+" stop")
        sys.stdout.close()

def ExecCmd():
        OldStdout=sys.stdout
        ExecCMS_AGT_DB("srocms")
        sys.stdout=OldStdout

#if __name__=="__main__":
        ExecCmd()
Community
  • 1
  • 1
Chirag
  • 607
  • 1
  • 6
  • 17

1 Answers1

3

Why don't you want to use subprocess? It's by far the simplest solution. See https://stackoverflow.com/a/3982683/2314532 for more complete details, but the gist of using subprocess.call() to redirect output boils down to:

f = open("outputFile","wb")
subprocess.call(argsArray,stdout=f)

So your posted code would become:

import subprocess

def ExecCMS_AGT_DB(cmd):
        outfile = open(cmd+'.txt','w')
        print "\ncmd $: "+cmd+" start"
        subprocess.call([cmd, "start"], stdout=outfile)
        print "\ncmd $: "+cmd+" stop"
        subprocess.call([cmd, "stop"], stdout=outfile)
        outfile.close()

def ExecCmd():
        ExecCMS_AGT_DB("srocms")

if __name__=="__main__":
        ExecCmd()

No need to save & restore sys.stdout, and nothing complicated. Just supply the parameter stdout to subprocess.call, and you've solved your problem. Easily, simply, and Pythonically.

Community
  • 1
  • 1
rmunn
  • 34,942
  • 10
  • 74
  • 105
  • The `print` statements wouldn't be redirected to that file anymore though, would they? I'm not sure how to allow two processes (the Python interpreter and the called subprocess) write access to the same file. – TigerhawkT3 Nov 23 '15 at 08:38
  • No, they wouldn't be redirected to that file. If that's what the OP wants to do, he should replace them with `outfile.write("\ncmd $: "+cmd+" start\n")` (note the `\n` at the end, which `print` adds automatically but `file.write()` does not). Assuming the `srocms` command isn't asynchronous, there's no problem with sharing access between the interpreter and the subprocess. (If it ***is*** asynchronous, the OP's code has many problems besides sys.stdout redirection...) – rmunn Nov 23 '15 at 08:42
  • @rmunn thanks for your help, but command passed in suprocess.call takes much more time to execute, so the next statement of it should wait until it completes, – Chirag Nov 23 '15 at 08:48
  • @Chirag - The docs for subprocess.call say "Run the command described by args. **Wait for command to complete**, then return the returncode attribute." Or do you mean that running `srocms start` appears to return instantly, but in reality it's running a lot of processing in the background? In that case your posted sample code is quite buggy, as `os.system` works exactly like `subprocess.call` in that regard. You will need to add the same "wait until it's really finished" logic to your original code as to my suggestion -- and `subprocess.call` is *still* better than `os.system` for your needs. – rmunn Nov 23 '15 at 09:02
  • @rmunn Yeah, I am implementing same with subprocess.call, thanks for your help and support – Chirag Nov 23 '15 at 09:05
  • @Chirag - Then what's probably going on is that the `srocms` program you're calling is forking and exiting immediately, while its forked child process (that does the real work) grabs a "real" stdout, ignoring what the parent program's stdout was. **Question:** What happens if you run, *on the command line*, something like `srocms > my_output.txt`? Does the `my_output.txt` file actually contain the redirected output, or not? If it doesn't, then my answer here won't help you either, and you need to find out if the `srocms` command has some command-line option like `-o` or `--output`. – rmunn Nov 23 '15 at 09:35
  • Hmm, the comment I was replying to just now has disappeared while I was typing. Did you get it working? – rmunn Nov 23 '15 at 09:35
  • @rmunn:: Sorry Sorry it was not stdout it was stderr. My mistake, – Chirag Nov 23 '15 at 09:36