user697911 user697911 - 6 days ago 6
Python Question

How to list files in a directory with yield

The iter doesn't run. I just want to print out all words for each file in the directory /tmp.

class CorpusReader:
def __init__(self, dirname):
self.dirname = dirname;

def __iter__(self):
for fname in os.listdir(self.dirname):
for line in open(os.path.join(self.dirname,fname)):
yield line.split()

reader = CorpusReader("/tmp")

Answer

What you need is to loop through reader:

for line in reader:
    print line

Update

Keep in mind that not all files are readable, you need to guard against this case:

    def __iter__(self):
        for fname in os.listdir(self.dirname):
            try:
                for line in open(os.path.join(self.dirname,fname)):
                    yield line.split()
            except IOError:
                pass  # or handle this error, most likely due to file not readable
Comments