I am currently struggling to find the number of words, characters, and lines of a song in a lyrics.txt file. I was able to find number of words and lines only, but the two code blocks I have for finding this out, doesn't work.
Here's my current code so far:
count = 0
infile = open('lyrics.txt')
for line in infile:
count = count + 1
print("The song has about", count, "lines in total.")
data = infile.read()
wordCount = len(data)
print("Love Me Harder has approximately", wordCount, "total words in the song.")
You can only "read" a file once. You are at the end of the file when you do `infile.read()`, so nothing is read.
You either have to re-open the file or call `infile.seek(0)` to force it back to the beginning of the file.