Skip to content Skip to sidebar Skip to footer

Unbound Variable Error With If Statements

I am getting 'UnboundLocalError: local variable 'fileName' referenced before assignment' when using variables underneath if/elif statements. The code takes a file name and uses a r

Solution 1:

Basically, because you have no else clause, if neither the if or the elif conditions evaluate to True, fileName is not defined. Perhaps try returning f in an else clause so that the function exits:

if f.endswith('mkv') and f.startswith('[H'):
    fileName = fileReg.findall(f)
elif f.endswith('mkv') and f.startswith('['):
    fileName=altReg.findall(f)
else:
    return f

Solution 2:

clearly the if and elif statements have not run and so fileName is not assigned yet so the error is simply saying-"this is not a variable"

Post a Comment for "Unbound Variable Error With If Statements"