Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
统计指定目录下的源文件行数
# -*- coding: utf-8 -*-
"""统计指定目录下,指定源文件类型的总行数"""
from collections import defaultdict
import os
d = defaultdict(int)
path = r"D:/vc++workspaces" #路径
f = ['.c','.cpp'] #需要统计源文件的类型
for dirpath,dirnames,filenames in os.walk(path):
for filename in filenames:
path = os.path.join(dirpath, filename)
ext = os.path.splitext(filename)[1]
if ext in f:
try:
d[ext] += len(list(open(path)))
except:
pass
for ext in f:
print (ext,d[ext])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment