-1
with open('WarningErr.txt', 'w') as err:
    subprocess.call(cmd, stderr=err)

with open('WarningOut.txt', 'w') as out:
    return_code = subprocess.call(cmd, stdout=out)

Right now, I write both errors and output in ".txt and stdout".

But it is too slow. I write in one, and then in another. I got to be able to write in both at the same time.

To replicate a little bit what the tee behaviour in Bash.

2 Answers2

0

I included tee in the command call like @Rightleg suggested. I change the command. But now it works perfectly.

I didn't think that I could call tee via the subprocess. But it works real well!

 # for example
 command = 'ls'

 # To write at the same time in the shell and a file both errors, and stdout (TEE behaviour)
 cmd = ['bash', '-c', '. myBash.bash; {} > >(tee {}/WarningOut.txt) 2> >(tee {}/WarningErr.txt >&2)'
       .format(command, PATHTOWHEREYOUWANTIT_g, PATHTOWHEREYOUWANTIT_g)]

return_code = subprocess.call(cmd)
0

guess you like to log to stdout/stderr and a file. Try the logging lib and this code:

import logging

# create logger obj
_glogger = logging.getLogger("myApp")

# create file sink
hdlr = logging.FileHandler(os.path.join("dumpDir",'myApp.log'))
formatter = logging.Formatter('%(asctime)s %(name)s[%(process)d] %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
_glogger.addHandler(hdlr)

# create steam sink = stdout
hdlr2 = logging.StreamHandler()
hdlr2.setFormatter(formatter)
_glogger.addHandler(hdlr2)
_glogger.setLevel(logging.DEBUG)

_glogger.debug("a debug message")
Stefan Jaritz
  • 1,999
  • 7
  • 36
  • 60