# example of reading mobydick-chap1.txt line by line
import re

infile = open('mobydick-chap1.txt', 'r')
wholefilestr = infile.read() # this will read the entire file into one string
infile.close()

outfile = open('md2.txt', 'w')

listofwords = wholefilestr.split() # by default split splits on whitespace

#listofwords = re.split('\s', wholefilestr)

#outfile.write(str(listofwords))
capwords = 0
for word in listofwords:
    # word is one element of listofwords
    if word[0] >= 'A' and word[0] <= 'Z':
        capwords += 1
        outfile.write(word + '\n')

outfile.close()

print(f'There were {capwords} capitalized words in the file.')
