Full script here: http://pastebin.com/d6isrghF
I’ll admit I’m very new to Python so please forgive my stupidity if this is an easy question to answer. The section in question is this:
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 is being fed to the script from sabnzbd and is:
/mnt/cache/.apps/sabnzbd/complete/name.of.folder
category is:
tv
So I guess my question is: why is this failing with the error?
Why it does not work
Your code cannot work because
whileis executed untilos.path.basename(sourcePath)is not evaluated toTrue, thenifstatement is called, which (because it looks like:if not os.path.basename(sourcePath)) is obviously evaluated asTrueand thus the message (your “error”) is shown:Annotated source code
When (and why) it sometimes works
It sometimes works only because category is found in the path, which means
whileloop will be exited (usingbreak) even despite still meeting criteria (the condition afterwhilekeyword:os.path.basename(sourcePath)). Because the condition fromwhileloop is still met (we exited the loop even though it was met), the next statement’s condition (not os.path.basename(sourcePath)) is no longer met and the message (“the error”) is not printed.Possible solutions
I believe one of the solutions is to add a counter to your code, that will print the error only if in specific number of iterations you will not be able to find what you needed. You can also try to catch “too many recursions” exception (if you will use recursion, of course, but the error will be like this:
RuntimeError: maximum recursion depth exceeded).However, you should redesign it rather to meet your own needs.