user697911
user697911

Reputation: 10531

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") 

Upvotes: 1

Views: 406

Answers (2)

Eli Sadoff
Eli Sadoff

Reputation: 7308

The second function, __iter__, allows an object of the class CorpusReader to be iterable. This means that you can iterate over it in a for loop (or other iterable methods). To print all the files do this

for i in reader:
    print(i)

Upvotes: 1

Hai Vu
Hai Vu

Reputation: 40723

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

Upvotes: 3

Related Questions