【python】统计某一路径下所有代码真实行数(空行已被过滤)

python学习网 2018-08-16 08:24:02

 1 #-*- coding:utf-8 -*-
 2 '''
 3 Created on 2018年8月15日
 4 
 5 @author: anyd
 6 '''
 7 import os
 8 list_line = []
 9 filepath = str(raw_input("请输入需要读取文件的路径:"))
10 for i,j,k in os.walk(filepath):
11     for file in k:
12         with open(filepath + file,'r') as f:
13             total_line = len(f.readlines())
14 #             print "该文件的总行数为:",total_line
15             f.close()
16         with open(filepath + file,'r') as f:
17             empty_line = f.readlines().count("\n")
18 #             print "该文件的总空行数为:",empty_line
19             f.close()
20         real_line = total_line - empty_line
21         list_line.append(real_line)
22         
23 sum_line = sum(list_line)
24 print sum_line

 

输出:

请输入需要读取文件的路径:D:\code\
796

阅读(789) 评论(0)