Python初學-----遍歷資料夾獲取資料夾下面檔案的所有行數
阿新 • • 發佈:2019-01-23
一次特殊需求,需要獲取專案裡面程式碼檔案的行數,一個指令碼搞定
import os
from os import path
rootPath=os.getcwd()
print(rootPath)
fileArray=[]
def getAllJavaFile(fileList,rootPath):
files=os.listdir(rootPath) #獲取檔案列表
for fileName in files:
fullpath=path.join(rootPath,fileName)
if(path.isdir(fullpath)): #如果是資料夾 遞迴出所有的檔案 新增到陣列中
getAllJavaFile(fileList,fullpath)
else:
if '.java' in fullpath or '.kt' in fullpath: #按需過濾自己想要識別的檔案
fileList.append(fullpath)
print(fullpath)
else:
pass
return fileList
lineCount=0
def readJavaFileLine(lineCount,fileArray) :
length=len(fileArray)
for i in range(0,length):
print("path = ",fileArray[i])
count = len(open(fileArray[i],'rb').readlines())
lineCount=lineCount+count
return lineCount
getAllJavaFile(fileArray,rootPath)
print(readJavaFileLine(lineCount,fileArray))