Find the number of characters in a file using Python

后端 未结 16 1444
春和景丽
春和景丽 2021-02-07 00:34

Here is the question:

I have a file with these words:

hey how are you
I am fine and you
Yes I am fine

And it is asked to find the numbe

16条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-07 01:26

    num_lines = sum(1 for line in open('filename.txt'))
    num_words = sum(1 for word in open('filename.txt').read().split())
    num_chars = sum(len(word) for word in open('filename.txt').read().split())
    

提交回复
热议问题