Search code examples
pythonlistdirectorysubdirectory

reading content of a directory and its subdirectories with python


i have directory path/to/dir that contains 10 subdirectories. there files in all of the subdirectories. I want to append these files in every subdirectory into a unique list. How can I do this?


Solution

  • What i understood is you just wanted to list all the filenames from a particular directory and its sub-directory.

    list1=[] #Empty list
    
    for root, dirs, files in os.walk(fr"path\\to\\directory", topdown=False): 
    #Listing Directory
        for name in files:
            a=os.path.join(root, name)
            print(a)
            list1.append(a)