我不明白这个...
无法运行此代码,我不知道为什么这是语法错误。
try:
newT.read()
#existingArtist = newT['Exif.Image.Artist'].value
#existingKeywords = newT['Xmp.dc.subject'].value
except KeyError:
print "KeyError"
else:
#Program will NOT remove existing values
newT.read()
if existingArtist != "" :
newT['Exif.Image.Artist'] = artistString
print existingKeywords
keywords = os.path.normpath(relativePath).split(os.sep)
print keywords
newT['Xmp.dc.subject'] = existingKeywords + keywords
newT.write()
except:
print "Cannot write tags to ",filePath
最后一个“except:”出现语法错误。再次...我不知道为什么 python 会抛出语法错误(在这个问题上花了 ~3 小时)。
最佳答案
else
之后不能有另一个except
。 try
、except
和 else
block 不像函数调用或其他代码 - 您不能随意混合和匹配它们喜欢。它总是一个特定的顺序:
try:
# execute some code
except:
# if that code raises an error, go here
# (this part is just regular code)
else:
# if the "try" code did not raise an error, go here
# (this part is also just regular code)
如果您想要捕获在 else
block 期间发生的错误,您将需要另一个 try
语句。像这样:
try:
...
except:
...
else:
try:
...
except:
...
仅供引用,如果您想捕获在 except
block 期间发生的错误,同样适用 - 在这种情况下,您还需要另一个 try
语句,例如这个:
try:
...
except:
try:
...
except:
...
else:
...
关于python - 尝试...否则...除了语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2792491/