t统计行数问题
import osfrom os.path import *
count=0
def list_check(dirlist):
for i in dirlist:
if isdir(i):
return list_check(i)
else:
with open(i,'r',encoding='utf-8') as f:
for j in f:
count+=1
def main():
dirlist=os.listdir('.')
list_check(dirlist)
print('总共有%d行代码'%count)
if __name__=='__main__':
main()
想统计代码得数量,报错了,求解
这样试试:
import os
from os.path import *
count = 0
def list_check(dirlist):
for i in dirlist:
if isdir(i):
return list_check(i)
else:
global count
try:
with open(i, 'r', encoding='utf-8') as f:
for j in f:
count += 1
except:
with open(i, 'r') as f:
for j in f:
count += 1
def main():
dirlist = os.listdir('.')
list_check(dirlist)
print('总共有%d行代码' % count)
if __name__ == '__main__':
main() zltzlt 发表于 2020-8-1 17:44
这样试试:
还是和上面一样的报错 fytfytf 发表于 2020-8-1 17:52
还是和上面一样的报错
发完整报错信息 本帖最后由 1q23w31 于 2020-8-1 18:32 编辑
@fytfytf
当前路径下有其他不是utf-8编码的文件吧,把这些文件移除,或者换个路径
亲测代码没问题
本帖最后由 1q23w31 于 2020-8-1 18:16 编辑
# -*- coding: utf-8 -*-
import os
from os.path import *
count=0
def list_check(dirlist):
global count
for i in dirlist:
if isdir(i):
return list_check(i)
else:
with open(i,'r',encoding='utf-8') as f:
for j in f:
count+=1
def main():
dirlist=os.listdir('.')
list_check(dirlist)
print('总共有%d行代码'%count)
if __name__=='__main__':
main()
static/image/hrline/line6.png
原始代码有一点问题,已帮您修改
页:
[1]