Python Forum
file tree recursion generator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
file tree recursion generator
#6
Why do you use your own complicated implementation (which could not handle WindowPaths) instead of using os.walk?


os.walk example as generator
import os
from pathlib import Path


def walk(path, maxdepth=None, sort_key=None, with_files=True, with_dirs=True):
    for depth, (root, dirs, files) in enumerate(os.walk(path)):
        if maxdepth is not None and depth > maxdepth:
            return
        if with_dirs:
            for directory in sorted(dirs, key=sort_key):
                yield Path(root, directory)
        if with_files:
            for file in sorted(files, key=sort_key):
                yield Path(root, file)
If you need compatibility to Python 2.7 (hopefully not), omit pathlib and use os.path.join.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
file tree recursion generator - by Skaperen - Feb-14-2020, 07:50 AM
RE: file tree recursion generator - by Gribouillis - Feb-15-2020, 11:07 AM
RE: file tree recursion generator - by Skaperen - Feb-16-2020, 03:56 AM
RE: file tree recursion generator - by kobibi11 - Jun-18-2020, 09:54 AM
RE: file tree recursion generator - by Skaperen - Jun-18-2020, 03:05 PM
RE: file tree recursion generator - by DeaD_EyE - Jun-18-2020, 04:12 PM
RE: file tree recursion generator - by Gribouillis - Jun-18-2020, 05:29 PM
RE: file tree recursion generator - by Skaperen - Jun-19-2020, 11:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  file tree walk depth first generator Skaperen 0 3,667 Aug-12-2019, 07:02 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020