PythonGenerator: Unterschied zwischen den Versionen
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…“) |
|||
(Eine dazwischenliegende Version desselben Benutzers wird nicht angezeigt) | |||
Zeile 10: | Zeile 10: | ||
class DirTree: | class DirTree: | ||
def | def traverse(self, path): | ||
for file in os.listdir(path): | |||
for file in os.listdir: | |||
yield path + "/" + file | yield path + "/" + file | ||
for | for aFile in os.listdir(path): | ||
if | if aFile != "." and aFile != "..": | ||
full = path + "/" + | full = path + "/" + aFile | ||
if os.path.isdir(full): | if os.path.isdir(full): | ||
self.traverse(full) | for item in self.traverse(full): | ||
yield item | |||
def main(argv): | def main(argv): | ||
aDir = DirTree( | aDir = DirTree() | ||
for full in aDir. | for full in aDir.traverse("/etc"): | ||
print(full) | print(full) | ||
if __name__ == " | if __name__ == "__main__" : | ||
main(sys.argv) | main(sys.argv) | ||
</pre> | </pre> |
Aktuelle Version vom 25. August 2016, 10:23 Uhr
Example for a Recursive Generator[Bearbeiten]
"""filetree.py: Implements a generator (iterator) for file trees. """ import os import os.path import re import sys class DirTree: def traverse(self, path): for file in os.listdir(path): yield path + "/" + file for aFile in os.listdir(path): if aFile != "." and aFile != "..": full = path + "/" + aFile if os.path.isdir(full): for item in self.traverse(full): yield item def main(argv): aDir = DirTree() for full in aDir.traverse("/etc"): print(full) if __name__ == "__main__" : main(sys.argv)