PythonGenerator: Unterschied zwischen den Versionen

Aus Info-Theke
Zur Navigation springen Zur Suche springen
(Die Seite wurde neu angelegt: „Kategorie:Python == Example for a Recursive Generator == <pre>"""filetree.py: Implements a generator (iterator) for file trees. """ import os import os.pat…“)
 
Zeile 31: Zeile 31:
         print(full)
         print(full)


if __name__ == "main" :
if __name__ == "__main__" :
     main(sys.argv)
     main(sys.argv)


</pre>
</pre>

Version vom 25. August 2016, 10:06 Uhr

Example for a Recursive Generator

"""filetree.py:
Implements a generator (iterator) for file trees.
"""
import os
import os.path
import re
import sys

class DirTree:
    def __init__(self, path):
        self._path = path
        
    def next():
        self.traverse(self._path)
        
    def traverse(path):
        for file in os.listdir:
            yield path + "/" + file
        
        for file in os.listdir:
            if file != "." and file != "..":
                full = path + "/" + file
                if os.path.isdir(full):
                    self.traverse(full)
        
def main(argv):
    aDir = DirTree("/etc")
    for full in aDir.next():
        print(full)

if __name__ == "__main__" :
    main(sys.argv)