All与ZIP联用,os.walk的使用

1
2
3
4
5
6
import re
sep = re.compile(r',\s*')
def _sep(s):
s = sep.sub(' ', s).strip()
return s.split(' ')

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)
#print (s)
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':
#try:
#with open(path) as py:
#print(path)
#print(list(py))
#s += _sum(py)
#except UnicodeDecodeError:
with open(path, encoding='utf-8') as py:
s += _sum(py)


All与ZIP联用,os.walk的使用
https://b.limour.top/130.html
Author
Limour
Posted on
May 30, 2020
Licensed under