0
def getFiles(_root):
    return next(os.walk(_root))[2]

root = "../../../Image/new_m/"
fileList = getFiles(root)  # return file list
fileList = sorted(fileList)

I get this error every time I try to run my function which is supposed to get files from my folder. The error shows StopIteration every time I try to run it.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    Maybe use `os.listdir` instead? Do you understand what `StopIteration` means? How to resolve it depends on what you expect to happen instead. – mkrieger1 Oct 09 '21 at 18:37
  • 1
    Possible duplicate of https://stackoverflow.com/questions/37289653/os-walk-error-of-unhandled-stopiteration – mkrieger1 Oct 09 '21 at 18:41
  • Possibly related: [Why am I getting a FileNotFoundError?](https://stackoverflow.com/q/17658856/4518341) – wjandrea Oct 09 '21 at 18:54
  • BTW, welcome to Stack Overflow! Please take the [tour] and read [ask]. – wjandrea Oct 09 '21 at 18:54
  • @mkrieger1 I want to run the code till it reaches its end in the directory. I know what os.walk means but i dont knw how to use it here. P.S. my program is aimed at identifying a fake currency for Indian bills using edge detection segmentation, etc. So if posssible reply I will share with you and resolve this – SANYAM CHAURASIA Oct 22 '21 at 08:30

1 Answers1

0
import os
def getFiles(_root):
    try:
        return next(os.walk(_root))[2]
    except StopIteration:
        return []  # Return an empty list if the iterator is empty

root = "../../../Image/new_m/"
fileList = getFiles(root)
fileList = sorted(fileList)

You can try the above-mentioned code. It will resolve your issue.

IAM5K
  • 257
  • 3
  • 10