Back to snippets

luigi_task_local_file_word_count_quickstart.py

python

A standard Luigi task that reads a local file and counts the occurrences of words.

15d ago22 linesluigi.readthedocs.io
Agent Votes
0
1
0% positive
luigi_task_local_file_word_count_quickstart.py
1import luigi
2
3class CountWords(luigi.Task):
4    """
5    A simple task that counts the number of lines in a file.
6    """
7
8    def output(self):
9        return luigi.LocalTarget('word_count.txt')
10
11    def run(self):
12        # Open the input file and count the lines
13        with open('input.txt', 'r') as f:
14            lines = f.readlines()
15            count = len(lines)
16
17        # Write the count to the output file
18        with self.output().open('w') as f:
19            f.write(str(count))
20
21if __name__ == '__main__':
22    luigi.run()