Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thomasnield/8c8dad7db46897e1972234986444f2d1 to your computer and use it in GitHub Desktop.
Save thomasnield/8c8dad7db46897e1972234986444f2d1 to your computer and use it in GitHub Desktop.
Iterating Files Recursively with RxPy
from rx import Observable
import os
def recursive_files_in_directory(folder):
def emit_files_recursively(observer):
for root, directories, filenames in os.walk(folder):
for directory in directories:
observer.on_next(os.path.join(root, directory))
for filename in filenames:
observer.on_next(os.path.join(root, filename))
observer.on_completed()
return Observable.create(emit_files_recursively)
recursive_files_in_directory('C:\\MyFolder') \
.filter(lambda f: f.endswith('.csv')) \
.flat_map(lambda f: Observable.from_(open(f))) \ # read lines
.subscribe(lambda l: print(l)) # print lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment