|
发表于 2019-8-20 16:54:27
|
显示全部楼层
需要安装 chardet 库 : pip install chardet
- import easygui as f
- import os
- import chardet
- filenames = []
- linenum = 0
- tallcountlines = 0
- texts = ''
- appendtexts = []
- types = ['.py', '.c', '.cpp', '.net', '.txt']
- typescount = {'.py': 0, '.c': 0, '.cpp': 0, '.net': 0, '.txt': 0}
- typesline = {'.py': 0, '.c': 0, '.cpp': 0, '.net': 0, '.txt': 0}
- filedirs = f.diropenbox('请选择你的代码库', '浏览文件夹')
- for root, dirs, filename in os.walk(filedirs):
- for name in filename:
- filepath = os.path.join(root, name)
- name3, isc = os.path.splitext(filepath)
- if isc in types:
- typescount[isc] = typescount.get(isc) + 1
- data = open(filepath, "rb").read()
- with open(filepath, encoding=chardet.detect(data)['encoding']) as texts:
- for line in texts.readlines():
- typescount[isc] = typescount.get(isc) + 1
- for keys in typesline.keys():
- tallcountlines += typesline[keys]
- texts = '【' + keys + '】' + '源文件' + str(typescount[keys]) + '个,源代码' + str(typesline[keys]) + '行\n'
- appendtexts = appendtexts.append(texts)
- titles = '你目前共累计编写了' + str(tallcountlines) + '行代码,完成度:' + str(tallcountlines / 100000) + '%\n离10行代码还差' + str(
- 100000 - tallcountlines) + '行,请继续努力!'
- showtext = f.textbox(titles, '统计结果', texts)
复制代码 |
|