【python】统计某一路径下所有代码行数(暂时还不能忽略空行^_^)

 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             lines = f.readlines()
14             count_line = len(lines)
15             list_line.append(count_line)
16             f.close()
17 total_line = sum(list_line)
18 print total_line

输出:

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

猜你喜欢

转载自www.cnblogs.com/51study/p/9482308.html