请教我的代码错字哪里?
请教我的代码错字哪里?
# -*- coding: utf-8 -*-
# 以下是统计程序代码行数的代码
# 我的目的是统计所有的 *.ini,*.bas,*.frm ...... 的文件总行数,
# 但是有一个问题:
# Traceback (most recent call last):
# File "D:\YwMis\tongji.py", line 20, in <module>
# data = f.read()
# File "C:\Python27\lib\codecs.py", line 671, in read
# return self.reader.read(size)
# UnicodeDecodeError: 'gbk' codec can't decode bytes in position 2-3: illegal multibyte sequence
# 不知以上提示是什么意思?
import os
import codecs
lines_count = 0
for roots,dirs,files in os.walk('d:/ywmis/'):
for file in files:
if file[-4:]=='.bas' or file[-4:]=='.frm' or file[-4:]=='.sql' or file[-4:]=='.cls' or file[-4:]=='.txt':
f = codecs.open(os.path.join(roots, file),'r','gbk')
data = f.read()
f.close()
lines_count += data.count('\n')
if not data.endswith('\n'):
lines_count += 1
print ("all lines count:%d" %lines_count)
import os
def filelines(filename):
content = open(filename).read()
lns = content.count('\n')
if not content.endswith('\n'):
lns += 1
return lns
def filefilter(filename):
return os.path.splitext(file)[-1] in ('.bas', '.frm', '.sql', '.cls', '.txt')
for roots, dirs, files in os.walk('d:/ywmis/'):
lines_count = sum(map(
lambda f: filelines(os.path.join(roots, file)),
filter(filefilter, files)
))
print ("all lines count:%d" %lines_count)
lines_count = 0
for roots, dirs, files in os.walk('d:/ywmis/'):
lines_count += sum(map(
lambda f: filelines(os.path.join(roots, f)), # file ==> f
filter(filefilter, files)))
import os
def filelines(filename):
content = open(filename,'r').read()
lns = content.count('\n')
if not content.endswith('\n'):
lns += 1
return lns
def filefilter(filename):
return os.path.splitext(filename)[-1] in ('.bas', '.frm', '.sql', '.cls', '.txt')
def trans_filename(filename):
pass
path='d:\\ywmis'
total=0
for roots, dirs, files in os.walk(path):
for file in files:
file=os.path.join(roots, file)
print(file)
if filefilter(file):
total+=filelines(file)
print(total)