|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
大概看了一下甲鱼的思路 然后自己写了写是这样
import easygui as g
import os
def show_result():
line = 0
total = 0
text = ''
for i in type_len:
line = type_len[i]
total += line
text += f'{i}类型代码有{file_list[i]}个,写了{type_len[i]}行\n'
title = '统计结果'
msg = f'一共写了{total}行代码,还差{100000-total}行'
g.textbox(msg, title, text)
def file_line_cal(file_name):
count = 0
with open(file_name) as f:
print(f'正在分析{file_name}')
try:
for each_line in f:
count += 1
except UnicodeDecodeError:
pass
return count
def search_file(path):
line = 0
os.chdir(path)
for each_file in os.listdir(path):
ext = os.path.splitext(each_file)[1]
if ext in target:
line = file_line_cal(each_file)
try:
file_list[ext] += 1
except KeyError:
file_list[ext] = 1
try:
type_len[ext] += line
print('a')
except KeyError:
file_list[ext] = line
if os.path.isdir(each_file):
search_file(each_file)
os.chdir(os.pardir)
target = ['.c', '.py']
file_list = {}
type_len = {}
file_path = g.diropenbox('选择代码库')
search_file(file_path)
show_result()
print(file_list, ' ', type_len)
为什么运行不出来呢
本帖最后由 Twilight6 于 2020-5-4 09:53 编辑
- import easygui as g
- import os
- def show_result():
- total = 0
- text = ''
- for i in type_len:
- line = type_len[i]
- total += line
- text += f'{i}类型代码有{file_list[i]}个,写了{type_len[i]}行\n'
- title = '统计结果'
- msg = f'一共写了{total}行代码,还差{100000-total}行'
- g.textbox(msg, title, text)
- def file_line_cal(file_name):
- count = 0
- with open(file_name) as f:
- print(f'正在分析{file_name}')
- try:
- for each_line in f:
- count += 1
- except UnicodeDecodeError:
- pass
- return count
- def search_file(path):
- os.chdir(path) # 这工作目录已经改成了你代码库的路径
- for each_file in os.listdir(os.curdir): # 这边改成当前目录就行,不用再使用path 的路径
- ext = os.path.splitext(each_file)[1]
- if ext in target:
- line = file_line_cal(each_file)
- try:
- file_list[ext] += 1
- except KeyError:
- file_list[ext] = 1
- try:
- type_len[ext] += line
- print('a')
- except KeyError:
- file_list[ext] = line
- if os.path.isdir(each_file):
- search_file(each_file)
- os.chdir(os.pardir)
- target = ['.c', '.py']
- file_list = {}
- type_len = {}
- file_path = g.diropenbox('选择代码库')
- print(file_path)
- search_file(file_path)
- show_result()
- print(file_list, ' ', type_len)
复制代码
不过你代码还是有问题,没有统计行数,你看看自己改改
|
|