I have written a python script and wanted to debug it using eric ide. When I was running it, an error popped up saying unhandled StopIteration
My code snippet:
datasetname='../subdataset'
dirs=sorted(next(os.walk(datasetname))[1])
I am new to python and so, I don't really know how to fix this. Why is this error popping up and how do I fix it?
os.walk
will generate file names in a directory tree walking it down. It will return the contents for every directory. Since it is a generator it will raiseStopIteration
exception when there's no more directories to iterate. Typically when you're using it in thefor
loop you don't see the exception but here you're callingnext
directly.If you pass non-existing directory to it will immediately raise the the exception:
You could modify your code to use
for
loop instead ofnext
so that you wouldn't have to worry about the exception:The other option is to use
try
/except
to catch the exception: