私は診断プログラムに似たプログラムを書いています。それはテストを実行し、それに基づいてさらにテストを実行するので、これらのほとんどは内部`try,except
で行われ、かなりの数があります。これを達成するが、数を減らす他の方法はありtry except
ますか?
これがサンプルコードです。
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"
上記の同じコード(ファイル名が異なる)が繰り返されますが、それを行うのは良い習慣ではないことを私は知っています。私はPythonにかなり慣れていないので、グーグルで役立つ結果は得られませんでした。