Search code examples
pythonarraysiterationgoogle-colaboratorystopiteration

os.walk error , can't fill list with file names


I'm learning "Google colab" and got some problem with data reading. I've written a python script for filling folders names into arrays, but it doesn't fill at even one.

TEST_PATH = "/content/gdrive/'My Drive'/test/"
test_ids= []
try:
    test_ids = next(os.walk(TEST_PATH))[1]
except StopIteration:
    pass

list "test_ids" still empty, but there are some folders with files in TEST_PATH: here you can check it


Solution

  • Change your path like this: TEST_PATH = "/content/gdrive/My Drive/test/"

    And to list files, I think it should be this: next(os.walk(TEST_PATH))[2].

    next(os.walk(TEST_PATH))[1] will list the folders.

    You can see more details in this answer: https://stackoverflow.com/a/10989155/11524628