不要以这种方式遍历文件。而是使用
for循环。
for line in f: vowel += sum(ch.isvowel() for ch in line)
实际上,您的整个程序就是:
VOWELS = {'A','E','I','O','U','a','e','i','o','u'}# I'm assuming this is what isvowel checks, unless you're doing something# fancy to check if 'y' is a vowelwith open('filename.txt') as f: vowel = sum(ch in VOWELS for line in f for ch in line.strip())就是说,如果
while由于某些误导的原因您真的想继续使用循环:
while True: line = f.readline().strip() if line == '': # either end of file or just a blank line..... # we'll assume EOF, because we don't have a choice with the while loop! break



