我有这个片段,我用来将图像文件转换为tiff.我希望在文件无法转换时收到通知.Imagemagick在成功运行时退出0,因此我认为以下代码段会报告该问题.但是根本没有报告任何错误.
def image(filePath,dirPath,fileUUID,shortFile):
try:
os.system("convert " + filePath + " +compress " + dirPath + "/" + shortFile + ".tif")
except OSError, e:
print >>sys.stderr, "image conversion failed: %s" % (e.errno, e.strerror)
sys.exit(-1)
Run Code Online (Sandbox Code Playgroud)
os.system()如果返回值不为零,则不会抛出异常.你应该做的是捕获返回值并检查:
ret = os.system(...)
if ret == ...:
Run Code Online (Sandbox Code Playgroud)
当然,你应该也做的是替换os.system()用subprocess.