Recursive os.listdir?

I would like to get a list of all the files in a directory recursively, without directories.

Say there the ~/files directory with "a.txt", "b.txt" and the "c" directory with "d.txt" and "e" inside it and "f.txt" inside e. How can I get a list, similar to ['/home/user/files/a.txt', '/home/user/files/b.txt', '/home/user/files/c/d.txt', '/home/user/files/c/e/f.txt'] ?

+6
source share
1 answer
 import os [os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser("~/files")) for f in fn] 
+22
source

Source: https://habr.com/ru/post/955732/


All Articles