python - Subprocess error file -


i'm using python module subprocess call program , redirect possible std error specific file following command:

with open("std.err","w") err:     subprocess.call(["exec"],stderr=err) 

i want "std.err" file created if there errors, using command above if there no errors code create empty file. how can make python create file if it's not empty?

i can check after execution if file empty , in case remove it, looking "cleaner" way.

you use popen, checking stderr:

from subprocess import popen,pipe  proc = popen(["exec"], stderr=pipe,stdout=pipe,universal_newlines=true)  out, err = proc.communicate() if err:     open("std.err","w") f:         f.write(err) 

on side note, if care return code should use check_call, combine namedtemporaryfile:

from tempfile import namedtemporaryfile os import stat,remove shutil import move  try:     namedtemporaryfile(dir=".", delete=false) err:         subprocess.check_call(["exec"], stderr=err) except (subprocess.calledprocesserror,oserror) e:     print(e)   if stat(err.name).st_size != 0:     move(err.name,"std.err") else:     remove(err.name) 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -