14

What is the simplest way to get the full recursive list of files inside a folder with python? I know about os.walk(), but it seems overkill for just getting the unfiltered list of all files. Is it really the only option?

dreftymac
  • 29,742
  • 25
  • 114
  • 177
static_rtti
  • 50,483
  • 45
  • 130
  • 185
  • Does this answer your question? [Recursive sub folder search and return files in a list python](https://stackoverflow.com/questions/18394147/recursive-sub-folder-search-and-return-files-in-a-list-python) – Tomerikoo Nov 16 '21 at 15:08

6 Answers6

18

There's nothing preventing you from creating your own function:

import os

def listfiles(folder):
    for root, folders, files in os.walk(folder):
        for filename in folders + files:
            yield os.path.join(root, filename)

You can use it like so:

for filename in listfiles('/etc/'):
    print filename
Lauritz V. Thaulow
  • 45,713
  • 12
  • 68
  • 88
Blender
  • 275,078
  • 51
  • 420
  • 480
12

os.walk() is not overkill by any means. It can generate your list of files and directories in a jiffy:

files = [os.path.join(dirpath, filename)
    for (dirpath, dirs, files) in os.walk('.')
    for filename in (dirs + files)]

You can turn this into a generator, to only process one path at a time and safe on memory.

Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
3

You could also use the find program itself from Python by using sh

import sh
text_files = sh.find(".", "-iname", "*.txt")
amoffat
  • 658
  • 4
  • 12
1

Either that or manually recursing with isdir() / isfile() and listdir() or you could use subprocess.check_output() and call find .. Bascially os.walk() is highest level, slightly lower level is semi-manual solution based on listdir() and if you want the same output find . would give you for some reason you can make a system call with subprocess.

kgr
  • 9,552
  • 2
  • 37
  • 42
1

pathlib.Path.rglob is pretty simple. It lists the entire directory tree

(The argument is a filepath search pattern. "*" means list everything)

import pathlib


for path in pathlib.Path("directory_to_list/").rglob("*"):
    print(path)
Miłosz Łakomy
  • 986
  • 4
  • 12
0
import os
path = "path/to/your/dir"
for (path, dirs, files) in os.walk(path):
    print files

Is this overkill, or am I missing something?

verbsintransit
  • 868
  • 3
  • 8
  • 17