37,720
社区成员
发帖
与我相关
我的任务
分享
import os
import os.path
import re
def countfiles(filename):
num = 0
if os.path.isfile(filename):
if not re.match('\S+[\.frm|\.bas]', filename):
return 0
for (i, line) in enumerate(open(filename)):
pass
print ("%s : %d lines" %(filename, i))
return i
for f in os.listdir(filename):
num += countfiles(os.path.join(filename, f))
return num
#!/usr/bin/env python
import os
exts = ('.bas', '.frm', '.sql', '.cls', '.txt', '.c', '.h')
def get_file_line_count(filename):
lines = 0
with open(filename, 'rb') as fd:
data = fd.read()
fd.close()
lines += data.count('\n')
if not data.endswith('\n'):
lines += 1
print("%s : %d" % (filename, lines))
return lines
def get_file(fpath):
lines = 0
for roots, dirs, files in os.walk(fpath):
for file in files:
filename = os.path.join(roots, file)
x, ext = os.path.splitext(file)
if ext in exts:
lines += get_file_line_count(filename)
print("all lines count: %d" % lines)
exts = ('.bas', '.frm', '.sql', '.cls', '.txt', '.c', '.h')
if __name__ == '__main__':
get_file('e:\project')
lines_count = 0
for roots,dirs,files in os.walk('.'):
for file in files:
if file[-4:]=='.frm' or file[-4:]=='.bas'
f = open(file)
data = f.read()
f.close()
lines_count += data.count('\n')
print "all lines count:%d" %lines_count
Traceback (most recent call last):
File "D:/ywgl2/a2.py", line 13, in <module>
data = f.read()
UnicodeDecodeError: 'gbk' codec can't decode bytes in position 598-599: illegal multibyte sequence
import os
lines_count = 0
for roots,dirs,files in os.walk('d:/ywgl/'):
for file in files:
if file[-4:]=='.ini':
f = open(file)
data = f.read()
f.close()
lines_count += data.count('\n')
print ("all lines count:%d" %lines_count)
Traceback (most recent call last):
File "C:/a.py", line 6, in <module>
f = open(file)
IOError: [Errno 2] No such file or directory: 'MisVer.ini'