Use python script to count the number of lines in a file

高洛峰
Release: 2016-10-20 10:10:43
Original
1519 people have browsed it

Python can count the number of lines in a file, do you believe it? Whether you believe it or not, I do. Let's take a look at how python counts the number of lines in a file. The code is very simple. I also made comments and it is very simple to implement. . .

1

2

3

4

5

6

7

8

9

#_*_coding:utf_8
#引入sys和系统os模块
import sys 
import os 
'''
计算某一个文件的行数
'''
def countFileLines(filename): 
    count = 0
    try: 
        handle = open(filename, 'r') 
        #循环文件句柄
for eachline in handle:
            #累加
            count += 1
    #异常捕获
    except IOError, e: 
        print 'file open error', e
    return count 
    
print countFileLines('/home/pythontab/tmp/a.php')
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template