importsubprocessprocess=subprocess.Popen(["executable","arg1","arg2",...,"argn"],# must be strings or pathlib.Path-like objects (with string representation)cwd=work_dir,# path to executable is always relative to cwd; executable is launched with cwd as working directory stdout=subprocess.PIPE,stderr=subprocess.PIPE)stdout,stderr=process.communicate()exit_code=process.returncode
importsubprocessfrompathlibimportPathdefexecute_binary(executable,arguments=[],work_dir='.'):ifnotarguments:arguments=[]ifnotwork_dir:work_dir='.'workdir=Path(work_dir).absolute()process=subprocess.Popen([executable]+list(str(arg)forarginarguments),cwd=work_dir,# path to executable is always relative to cwd; executable is launched with cwd as working directory stdout=subprocess.PIPE,stderr=subprocess.PIPE)stdout,stderr=process.communicate()exit_code=process.returncodeifexit_code:print(f"ERROR {exit_code}: something has gone wrong while executing the binary!")else:print(f"INFO: all went well while executing the binary!")print(f"STDOUT: {stdout.decode('ascii')}")print(f"STDERR: {stderr.decode('ascii')}")defexecute_binary_2(executable,*args,**kwargs):work_dir=Path(kwargs.get("work_dir",".")).absolute()print(args)print(kwargs)print(work_dir)process=subprocess.Popen([executable]+list(str(arg)forarginargs),cwd=work_dir,stdout=subprocess.PIPE,stderr=subprocess.PIPE)stdout,stderr=process.communicate()exit_code=process.returncodeifexit_code:print(f"ERROR {exit_code}: something has gone wrong while executing the binary!")else:print(f"INFO: all went well while executing the binary!")print(f"STDOUT: {stdout.decode('ascii')}")print(f"STDERR: {stderr.decode('ascii')}")