1. 程式人生 > >python 統計檔案行數

python 統計檔案行數

python 統計檔案行數

2017年08月30日 12:32:04 閱讀數:1630

版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/qq_29422251/article/details/77713741

 

 

方法1

count = len(open(filepath, 'r').readlines())

這種方法簡單,但是可能比較慢,當檔案比較大時甚至不能工作。

 

方法2

可以利用enumerate(),統計檔案函式:

 
  1.  
  2. count = 0

  3. for index, line in enumerate(open(filepath,'r')):

  4.     count += 1

  5. print count

  6.  
  7.