Search code examples
pythondirectoryoperating-systemsubdirectory

Python OS - using 'os.listdir' to view a directory that returns a list of folders. How do I view the contents of the subfolders?


I have been set a challenge to browse a directory that contains mostly empty folders - the flag(answer) is in one of them. I have used os module to see all the names of the folders - they are all named 'folder-' plus a number between 1 and 200. How do I view what is inside them?


Solution

  • You should use os.walk() instead of litdir() like as

    import os
    import os.path
    
    for dirpath, dirnames, filenames in os.walk("."):
        for file in filenames:
            print(file)