1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import os, re
directory = r'H:\课件\Python' space = re.compile(r'\s')
def _sum(py): temp = (space.sub('', line) for line in py) s = sum(line[0] != '#' for line in temp if line) return(s)
s = 0 for p in os.walk(directory): for f in p[2]: path = os.path.join(p[0],f) fname,ext = os.path.splitext(f) if ext == '.py': with open(path, encoding='utf-8') as py: s += _sum(py)
|