可能重複:
How to get line count cheaply in Python?如何打印/返回的行數在一個文件中
我想打印有在文件中有多少行。
我到目前爲止是這樣的,但它打印每一行的數量,我不知道如何讓Python只打印最後一個。
filename = input('Filename: ')
f= open(filename,'r')
counter = 1
line = f.readline()
while(line):
clean_line = line.strip()
print(counter)
line = f.readline()
counter += 1
f.close()
謝謝!不知道我可以在這裏使用len函數! –