我的代码产生错误输出时遇到问题。
def main():
count = 1
filename = input('Enter filename: ')
for lines in open(filename):
lines.strip('')
print('Total # of characters: ',len(lines))
count = count + 1
print('Total number of lines =', count)
main()
问题 - 编写一个程序来读取文件 words.txt,该文件每行一个单词并打印出文件中的字符总数和总行数。
因此,我的代码使用 count 来计算文件中将在最后打印的行数。这个计数工作正常。但是,计算字符是错误的。我的输出...
Enter filename: word.txtEnter filename: word.txt
Total # of characters: 3
Total # of characters: 6
Total # of characters: 5
Total # of characters: 6
Total # of characters: 6
Total number of lines = 5
word.txt 文件 =
hi
hello
hiiiiiiii
herrooo
herr