文章来自《Python cookbook》. 翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!
-- 61.182.251.99 [DateTime(2004-09-21T18:36:08Z)] TableOfContents
描述
统计文件行数
问题 Problem
需要计算文件的行数
解决 Solution
- The simplest approach, for reasonably sized files, is to read the file as a list of lines so that the count of lines is the length of the list.
对具有适当大小文件,最简单方法是拷贝文件各行到一个list中,list的长度就是文件的行数。
- If the file's path is in a string bound to the thefilepath variable, that's just:
如果文件路径由下面的string变量thefilepath给出,代码如下:
count = len(open(thefilepath).readlines( ))
...
讨论 Discussion
...
