Bottom Page

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)
rootVIII likes this post
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
How does this compare to os.walk()?
Quote
#3
it gets the files in the correct order. you can also provide your own sort key that will be applied to the list of names in each directory.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote

Top Page

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

Forum Jump:


Users browsing this thread: 1 Guest(s)