I'm writing a program similar to a diagnostic program , that runs a test and then based on that do more tests, so most of these are done inside `try,except and there are quite a lot of them. Is there any other way to achieve this but reduce the number of try except?
Here is a sample code.
try: treeinfo = subprocess.check_output(['C:\Python27\Scripts\scons.bat','-f' ,'scons_default.py' ,'--tree=all']) print "\n" print "Your machine type is ",platform.machine() print "Compiling using default compiler\n" print treeinfo except subprocess.CalledProcessError as e: print "ERROR\n" try: with open ('helloworld.exe')as f: subprocess.call('helloworld.exe') print"Build success" log64 = subprocess.check_output(["dumpbin", "/HEADERS", "helloworld.exe"]) if arch64 in log64: print "Architecture of the compiled file is 64-bit " elif arch32 in log64: print "Architecture of the compiled file is 32-bit" except IOError as e: print "Build failed\n" print "\n" The same code above (with different file names) repeat and I know that it's not a good practice to do that. I'm pretty new to python and googling didn't give any helpful results.
helloworld.exein order to execute it. Also, do you want the program to continue execution when it has encountered an error?subprocess.callwill throw an exception if the program doesn't exist.try: with open ('helloworld.exe')as f:just gotry: subprocess.call('helloworld.exe') ?