This examples shows a list comprehension that builds a list of all the files in a folder and its subfolders.

Create a flat list of all the files in subfolders in python including the path.

If you want to process all the files in a folder and its subfolders, you want a list like this:
a/b.txt
a/c.txt
a/d/e.txt
a/d/f.txt

Here is a list comprehension that creates that list

import os

assets_directory = "assets"

files = [os.path.join(root, name) for root, dirs, files in os.walk(assets_directory) for name in files]
for f in files:
    print(f)

Output:

assets/util.js
assets/favicon.png
assets/mywork/index.html
assets/mywork/trainingcover.png
assets/css/adapt.css
assets/css/screen.css
Written by Loek van den Ouweland on 2021-08-10.
Questions regarding this artice? You can send them to the address below.