Python Forum
file tree recursion generator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
file tree recursion generator
#1
this is a generator (usable as an iterator) that generates individual file system object paths in the tree topped by the given apex path. i plan the make a wrapper that yields tuples describing the file system object (type, size, times, owner, etc.). you can also download this source file from http://ipal.net/src/python/gen/ftrgen.py

from os import listdir,readlink
def ftrgen(path,**kwargs):
    """File tree recursion generator that yields all paths, """ \
    """optionally sorted by sortkey in key=.

Copyright © 2020, by Phil D. Howard - all other rights reserved

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

The author may be contacted by decoding the number
11054987560151472272755686915985840251291393453694611309
(provu igi la numeron al duuma)
"""
    maxdepth = kwargs.pop('depth',2**62)
    key = kwargs.pop('key',None)
    if kwargs:
        raise TypeError('ftrgen: '+__name__+'() '+\
            'got an unexpected keyword argument '+\
            ','.join([repr(x) for x in kwargs.keys()]))
    if not isinstance(path,str):
        raise TypeError('ftrgen: '+__name__+'() '+\
            'top/apex path is not a string (only str)')
    work = [[path]]
    deep = 0
    while work[0]:
        if work[-1]:
            path = '/'.join([x[0] for x in work])
            yield path
            try:
                readlink(path)
                names = []
            except OSError:
                try:
                    names = sorted(listdir(path),key=key)
                except OSError:
                    names = []
                deep += 1
                if deep > maxdepth:
                    names = []
            if names:
                work.append(names)
            else:
                work[-1].pop(0)
            continue
        else:
            work[-1:] = []
            work[-1].pop(0)
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
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,037 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