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

时间:2022-01-06 14:09:34
 
 #-*- coding:utf-8 -*-
'''
Created on 2018年8月15日 @author: anyd
'''
import os
list_line = []
filepath = str(raw_input("Please input the code path:"))
for i,j,k in os.walk(filepath):
for file in k:
with open(filepath + file,'r') as f:
total_line = len(f.readlines())
# print "该文件的总行数为:",total_line
with open(filepath + file,'r') as f:
empty_line = f.readlines().count("\n")
# print "该文件的总空行数为:",empty_line
real_line = total_line - empty_line
list_line.append(real_line) sum_line = sum(list_line)
print "CCT result is:",sum_line

输出:

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