批量计算文件夹下所有代码总行数

使用如下python代码:

import os

dir = os.getcwd()  #获取当前目录
subdirlist = os.listdir(dir)  #获取当前目录下文件列表
total = 0
for subdir in subdirlist:
    path = os.path.join(dir, subdir)
    print(path)
    with open(path) as f:
        line = len(f.readlines())
    total = total + line
print("total lines num: %d"%total)
posted @ 2023-04-20 16:43  snail1502  阅读(61)  评论(0)    收藏  举报