Python 获取某个文件夹下的所有代码总行数
1 #获取某个文件夹下的所有代码总行数 2 import os 3 def GetAllPath(dirname): 4 result = [] 5 for maindir, subdir, file_name_list in os.walk(dirname): 6 for filename in file_name_list: 7 apath = os.path.join(maindir, filename) 8 result.append(apath) 9 print("result",len(result)) 10 return result 11 12 def GetExtNamesList(fileslist, ext): 13 filenames = [] 14 for file in fileslist: 15 fileinfo = os.path.splitext(file) 16 if fileinfo[1] == ext: 17 filenames.append(file) 18 print("filenames", len(filenames)) 19 return filenames 20 21 def GetFileLines(filename): 22 with open(filename, 'r', encoding='UTF-8', errors='ignore') as file: 23 file_list = file.readlines() 24 lines = len(file_list) 25 return lines 26 27 def GetTotalLines(filelist): 28 totalline=0 29 for file in filelist: 30 if os.path.exists(file): 31 totalline = totalline + GetFileLines(file) 32 return totalline 33 34 sourcefolder = r'E:\xxxx\xxxx' 35 filelist = GetExtNamesList(GetAllPath(sourcefolder), '.cs') 36 alllines=GetTotalLines(filelist) 37 print("总行数 ", alllines)