我正在写一个脚本,在我的脚本中我有这个功能:
def insert_image(cursor, object_id, sku):
product_obj = core.Object.get(object_id)
string_sku = str(sku)
folder = string_sku[0] + string_sku[1] + string_sku[2]
found_url = False
# KLUDGE This is ugly and redundant, however putting this in an if elif elif else throws error when url not found
# try this url first
try urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):
urllib.URLopener().retrieve("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku), "%sPR-IT,PM.jpg" % (sku))
found_url = True
except:
found_url = False
# If that one didn't work try this one
if found_url == False:
try urllib.urlopen("http://<path to images>/%s/%sPK-PT,PM.jpg" % (folder, sku)):
urllib.URLopener().retrieve("http://<path to images>/%s/%sPK-PT,PM.jpg" % (folder, sku), "%sPK-PT,PM.jpg" % (sku))
found_url = True
except:
found_url = False
# If still nothing, one last attempt
if found_url == False:
try urllib.urlopen("http://<path to images>/%s/%sCC-PT,IM.jpg" % (folder, sku)):
urllib.URLopener().retrieve("http://<path to images>/%s/%sCC-PT,IM.jpg" % (folder, sku), "%sCC-PT,IM.jpg" % (sku))
found_url = True
except:
found_url = False
# We failed to find an image for this product, it will have to be done manually
if found_url == False:
log.info("Could not find the image on notions")
return False
# Hey we found something! Open the image....
send_image = open('%sPK-PT,PM.jpg' % sku, 'r')
# ...and send it for processing
if product_obj.set_image(send_image, 5, 1) == False:
return False
else:
log.debug("Inserted Image")
return True
这个工作正常,直到我添加了试试捕获。我确实有if,elif,函数运行得很好。这是我的电话和在它之前运行的代码:
if rollback == False:
# Nah -- it's all good SAVE IT!
count += 1
log.debug("INSERT %s" % count)
conn.commit()
else:
# Yeah something went wrong, errors reported why, roll it back
conn.rollback()
log.debug("skipped %s" % skip_count)
# Insert images
if rollback == False:
sku = row[0]
if insert_image(cursor, object_id, sku) == False:
log.error("Could not get the image inserted for product: %s" % object_id)
conn.rollback()
else:
conn.commit()
我的错误是:
16:33:46,153 DEBUG [pylons-admin] Inserted Description
16:33:46,164 DEBUG [pylons-admin] Inserted Attributes
16:33:46,164 DEBUG [pylons-admin] INSERT 1
Traceback (most recent call last):
File "<console>", line 47, in <module>
NameError: name 'insert_image' is not defined
我不知道47行是什么意思,因为调用是在2101行,再次添加trys之前,它发现函数就好了。当我在调用insert_image之后提交之前添加了你现在看到的trys时,我还将第一次提交切换到insert_image调用之前。我检查了缩进,空格和标签没有用。
我使用TextMate,当我从TextMate运行脚本时,我在这里遇到语法错误:
try urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):
它指向(on(文件夹......但我没看到我在哪里有语法错误。请帮助。我已经在这个脚本上工作了几个星期了,这应该是最后一次测试并调用它完成:(
答案 0 :(得分:3)
您的函数中存在语法错误:
try urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):
urllib.URLopener().retrieve("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku), "%sPR-IT,PM.jpg" % (sku))
found_url = True
except:
found_url = False
应该是:
try:
urllib.urlopen("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku)):
urllib.URLopener().retrieve("http://<path to images>/%s/%sPR-IT,PM.jpg" % (folder, sku), "%sPR-IT,PM.jpg" % (sku))
found_url = True
except:
found_url = False
你也有一些广泛的捕获,捕获那些语法错误并隐藏错误,但insert_image没有这样定义。永远不要单独使用except:
,始终放置要捕获的异常名称。否则你也会遇到像SyntaxError这样的东西,这是非常危险的。
答案 1 :(得分:0)
这听起来像你在方法之前有空白问题。不正确的空格将它移到了正常的代码路径之外,如果它在一个类中,它可能看起来不再是在类中