![]() |
flattree.py - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: flattree.py (/thread-7669.html) |
flattree.py - Skaperen - Jan-20-2018 this is my code to generate a "flat" iteration of a file tree, without doing recursion. it can also be downloaded here, here, and here. #!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import division, generators, print_function, with_statement r"""Implement and demonstrate a flat file tree generator. file flattree.py purpose implement and demonstrate a flat file tree generator email 10054452614123394844460370234029112340408691 The intent is that this module and command works correctly under both Python 2 and Python 3. Please report failures or code improvement to the author. """ #-------#-------#-------#-------#-------#-------#-------#-------#-------#-------# __license__ = r""" Copyright © 2018, 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 10054452614123394844460370234029112340408691 (provu igi la numeron al duuma) """ #-------#-------#-------#-------#-------#-------#-------#-------#-------#-------# from os import environ, listdir, lstat, path from stat import S_ISDIR as isdir from sys import argv, stderr, stdin, stdout, version_info if version_info.major < 3: strs = (bytearray,str,unicode) if version_info.major > 2: strs = (bytes,bytearray,str) #-------#-------#-------#-------#-------#-------#-------#-------#-------#-------# def flat_tree(t,sort=False): if not isinstance(t,strs): raise TypeError dosort = bool(sort) q = [ ['',[t]] ] while q: b = q[-1][1] if b: n = b.pop(0) z = '/'.join([x[0] for x in q[1:]]+[n]) try: s = lstat(z) except: s = None if s and isdir(s.st_mode): try: l = listdir(z) except: l = [] if dosort and l: l = sorted(l) yield (z,s,l,) q.append([n,l]) else: yield (z,s,None) else: del q[-1] #-------#-------#-------#-------#-------#-------#-------#-------#-------#-------# def main(args): recs = [] for arg in args[1:]: n = 0 for p,s,f in flat_tree(arg,sort=True): print(path.normpath(p)) n += 1 print('there are',n,'files',file=stderr) return 0 #-------#-------#-------#-------#-------#-------#-------#-------#-------#-------# if __name__ == '__main__': if version_info.major < 3: BrokenPipeError = IOError try: result = main(argv) stdout.flush() except BrokenPipeError: result = 99 except KeyboardInterrupt: print('') result = 98 if result is 0 or result is None or result is True: exit(0) if result is 1 or result is False: exit(1) if isinstance(result,str): print(result,file=stderr) exit(2) try: exit(int(result)) except ValueError: print(str(result),file=stderr) exit(3) except TypeError: exit(4) #-------#-------#-------#-------#-------#-------#-------#-------#-------#-------# # 111111111122222222223333333333444444444455555555556666666666777777777788 #23456789012345678901234567890123456789012345678901234567890123456789012345678901 #=======#=======#=======#=======#=======#=======#=======#=======#=======#=======# # EOF RE: flattree.py - Larz60+ - Jan-20-2018 https://news.ycombinator.com/item?id=3881171 RE: flattree.py - Skaperen - Jan-21-2018 i don't follow. the referenced discussion made no sense to me from lack of context. |