You can use Popen by checking stderr:
from subprocess import Popen,PIPE proc = Popen(["EXEC"], stderr=PIPE,stdout=PIPE,universal_newlines=True) out, err = proc.communicate() if err: with open("std.err","w") as f: f.write(err)
On the side of the note, if you need a return code, you should use check_call , you can combine it with NamedTemporaryFile :
from tempfile import NamedTemporaryFile from os import stat,remove from shutil import move try: with NamedTemporaryFile(dir=".", delete=False) as err: subprocess.check_call(["exec"], stderr=err) except (subprocess.CalledProcessError,OSError) as e: print(e) if stat(err.name).st_size != 0: move(err.name,"std.err") else: remove(err.name)
source share