Parallel Python

countlines.py
import sys
from functools import reduce
from pathlib import Path

def count_lines_in_file(file):
    """
    Calculate the number of lines of text in a single file
    """
    text = file.read_text()
    lines = text.split("\n")
    return len(lines)

# get all of the names of the plays from the command line
files = sorted(Path("shakespeare").glob("*"))

# map the count_lines function against all of the
# files listed in "filenames"
play_line_count = sorted(map(count_lines_in_file, files))

# print out the filenames of the plays along with their line counts
for f, count in zip(files, play_line_count):
    print(f"{f} has {count} lines")

total = reduce(lambda x,y: x+y, play_line_count)

print("The total number of lines is %s." % total)
$
python countlines.py shakespeare
shakespeare/README has 3 lines
shakespeare/allswellthatendswell has 2938 lines
shakespeare/antonyandcleopatra has 3116 lines
shakespeare/asyoulikeit has 3400 lines
shakespeare/comedyoferrors has 3606 lines
shakespeare/coriolanus has 3768 lines
shakespeare/cymbeline has 3872 lines
shakespeare/hamlet has 3877 lines
shakespeare/juliuscaesar has 3884 lines
shakespeare/kinglear has 3974 lines
shakespeare/loveslabourslost has 4018 lines
shakespeare/macbeth has 4064 lines
shakespeare/measureforemeasure has 4108 lines
shakespeare/merchantofvenice has 4123 lines
shakespeare/merrywivesofwindsor has 4149 lines
shakespeare/midsummersnightsdream has 4336 lines
shakespeare/muchadoaboutnothing has 4338 lines
shakespeare/othello has 4449 lines
shakespeare/periclesprinceoftyre has 4516 lines
shakespeare/romeoandjuliet has 4644 lines
shakespeare/tamingoftheshrew has 4767 lines
shakespeare/tempest has 5425 lines
shakespeare/timonofathens has 5444 lines
shakespeare/titusandronicus has 5486 lines
shakespeare/troilusandcressida has 5526 lines
shakespeare/twelfthnight has 5837 lines
shakespeare/twogentlemenofverona has 5999 lines
shakespeare/winterstale has 6046 lines
The total number of lines is 119713.