python老版课后作业35最后一题
大概看了一下甲鱼的思路 然后自己写了写是这样import easygui as g
import os
def show_result():
line = 0
total = 0
text = ''
for i in type_len:
line = type_len
total += line
text += f'{i}类型代码有{file_list}个,写了{type_len}行\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)
if ext in target:
line = file_line_cal(each_file)
try:
file_list += 1
except KeyError:
file_list = 1
try:
type_len += line
print('a')
except KeyError:
file_list = 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)
为什么运行不出来呢{:5_104:} 本帖最后由 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
total += line
text += f'{i}类型代码有{file_list}个,写了{type_len}行\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)
if ext in target:
line = file_line_cal(each_file)
try:
file_list += 1
except KeyError:
file_list = 1
try:
type_len += line
print('a')
except KeyError:
file_list = 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)
不过你代码还是有问题,没有统计行数,你看看自己改改 Twilight6 发表于 2020-5-4 09:48
不过你代码还是有问题,没有统计行数,你看看自己改改
谢谢老哥!
页:
[1]