这里的完整脚本:http://pastebin.com/d6isrghF
我承认我是的新手,所以如果这是一个很容易回答的问题,请原谅我的愚蠢。有关的一节是:
sourcePath = jobPath
while os.path.basename(sourcePath):
if os.path.basename(os.path.dirname(sourcePath)).lower() == category.lower():
break
else:
sourcePath = os.path.dirname(sourcePath)
if not os.path.basename(sourcePath):
print "Error: The download path couldn't be properly determined"
sys.exit()jobPath是从sabnzbd提供给脚本的,并且是:
/mnt/cache/.apps/sabnzbd/complete/name.of.folder类别是:
tv所以我想我的问题是:为什么这个错误失败了?
发布于 2012-10-21 06:56:39
为什么不起作用
您的代码无法工作,因为while被执行,直到os.path.basename(sourcePath)未被计算为True,然后调用if语句,该语句(因为它看起来是:if not os.path.basename(sourcePath))显然被计算为True,从而显示消息(您的“错误”):
带注释的源代码
sourcePath = jobPath
# This is executed until os.path.basename(sourcePath) is evaluated as true-ish:
while os.path.basename(sourcePath):
if os.path.basename(os.path.dirname(sourcePath)).lower() == category.lower():
break
else:
sourcePath = os.path.dirname(sourcePath)
# Then script skips to the remaining part, because os.path.basename(sourcePath)
# has been evaluated as false-ish (see above)
# And then it checks, whether os.path.basename(sourcePath) is false-ish (it is!)
if not os.path.basename(sourcePath):
print "Error: The download path couldn't be properly determined"
sys.exit()当(和为什么)它有时起作用
有时,这只是因为在路径中找到了类别,这意味着while循环将被退出(使用break),即使仍然满足条件( while关键字后面的条件:os.path.basename(sourcePath))。因为仍然满足来自while循环的条件(即使满足了循环,我们也退出了循环),所以下一个语句的条件(not os.path.basename(sourcePath))不再满足,消息(“错误”)也不会被打印出来。
可能的解决办法
我相信其中一个解决方案是在您的代码中添加一个计数器,只有在特定次数的迭代中您无法找到所需的内容时,才会打印错误。您也可以尝试捕获“太多递归”异常(当然,如果要使用递归,但是错误将是这样的:RuntimeError: maximum recursion depth exceeded)。
然而,你应该重新设计它,而不是满足你自己的需要。
https://stackoverflow.com/questions/12995635
复制相似问题